Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to access nested objects, in the way I have my json formatted? #814

Open
jp-son opened this issue Jun 28, 2018 · 9 comments

Comments

@jp-son
Copy link

jp-son commented Jun 28, 2018

{
    "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.

@JoshuaRabiu
Copy link

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

@schwiet
Copy link

schwiet commented Jul 3, 2018

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

@ZenwalkerD
Copy link

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.

@oscarfroberg
Copy link

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.

@Glinkis
Copy link

Glinkis commented Jan 25, 2019

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

@b4dnewz
Copy link

b4dnewz commented Feb 20, 2019

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

@naythanwilliams
Copy link

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.

@kapil0jaiswal
Copy link

kapil0jaiswal commented Jul 29, 2020

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

@AlphonsG
Copy link

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.

I agree, either the devs are stupid for intentionally imposing this restriction or they naively wrote garbage code that caused it. For anyone looking for a workaround I've been using a python package I found that supports nesting: https://github.com/gera2ld/json-server.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants