Json-server: Is it possible to access nested objects, in the way I have my json formatted?

Created on 28 Jun 2018  路  8Comments  路  Source: typicode/json-server

{
    "results": {
        "values": {
            "123": {
                "name": "John"
                "age": 40
                },
            "456": {
                "name": "Bob"
                "age": 20
                },
            "789": {
                "name": "Anna"
                "age": 18
                }
        }
    }
}

Right now, on my json server, all I can access is the results object. But how can I specifically go into the values object, and pick one of its objects. Like how would I access "123" and its properties? Please help me out.

Most helpful comment

I wholly agree with a lot of the comments here. The disallowance of accessing properties on an object - say /api/posts/1/whatever - is almost unbelievable. It makes the json-server basically unusable for developing anything like a real product. There are so many obvious situations in which that would be essential. I honestly can't imagine why the developers would restrict it in the way they have.

All 8 comments

To access nested object properties, you would access each nested object by its property name, for example:
fetch('http://localhost:8000/results')
.then(res => res.json())
.then(res => console.log(res.values['123'].name))
which outputs"John"

The 123 object was accessed using bracket notation because it is a number and using dot notation would cause an error to throw.
Look at the documentation for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors

Also, it seems that you're missing commas after each name value, so that could be causing some issues

In OP's example, why doesn't json-server create resources other than for the top level object? For example, why does it not create:
http://localhost/results/values/123

In OP's example, why doesn't json-server create resources other than for the top level object? For example, why does it not create:
http://localhost/results/values/123

Yes, definitely helps to use it as REST API in my app. At the moment, such URL/RAPI doesnt work.
Any workaround ?

My JSON looks like this:

 {
  "productName": "Pipe",
  "productCode": "1-",
  "productAvailable": true,
  "productPrice": 122,
  "productRating": 4,
  "productDescription": "The Savinelli Tundra Brownblast Briar Pipe series have a very natural almost forest/woodland bushcraft look about them. Savinelli pipes are handmade in Milan Italy, and they are one of the oldest independent brands and one of the largest producers of smoking pipes today.Handcrafted from Briarwood the Savinelli Tundra Brownblast pipes have outstanding beauty. A light sandblasting technique is used to achieve this beautiful bowl then the dark brown stain highlights it's natural character. Finished with a custom made acrylic Cumberland fishtail mouthpiece. One characteristic of Savinelli pipes is the Balsa System filters that clean the smoke without altering the flavour. Of course, the pipes can be used without filter too. The Savinelli Tundra series takes a 9mm Balsa Wood Filter and within the box is a pack of five to get you started.",
  "productImageURL": "170767/1340130900.svg",
  "productComments": [
    {
      "id": 0,
      "dislikes": 0,
      "likes": 0,
      "comment": "asd"
    },
    {
      "id": 1,
      "dislikes": 0,
      "likes": 0,
      "comment": "asdaddsadadasd"
    }
  ]
}

via REST API, i cant access productComments object via URL like http://localhost:3000/products/1-/productComments
Please help.

In OP's example, why doesn't json-server create resources other than for the top level object? For example, why does it not create:
http://localhost/results/values/123

Yes, this was why I was expecting to be able to do. Then I could've quickly put together some dummy data to simulate a "real" API. Will need to look elsewhere for now, unfortunately.

This issue makes json-server unusable for a lot of API designs. Does anyone have a solution, or perhaps another server that allows this?

you should look at this comment by the author, maybe you'll find it helpful

I wholly agree with a lot of the comments here. The disallowance of accessing properties on an object - say /api/posts/1/whatever - is almost unbelievable. It makes the json-server basically unusable for developing anything like a real product. There are so many obvious situations in which that would be essential. I honestly can't imagine why the developers would restrict it in the way they have.

Yes, we cannot use nested objects but for the purpose of testing we can use custom routes and access that data as seperately
With customized prefix
--routes routes.json
to add custom routes

https://github.com/typicode/json-server#routes

Was this page helpful?
0 / 5 - 0 ratings

Related issues

goldmont picture goldmont  路  3Comments

TXRRNT picture TXRRNT  路  4Comments

jasonlimantoro picture jasonlimantoro  路  4Comments

0plus1 picture 0plus1  路  3Comments

Isanderthul picture Isanderthul  路  3Comments