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

Accessing Nested Values #359

Closed
xseano opened this issue Sep 15, 2016 · 6 comments
Closed

Accessing Nested Values #359

xseano opened this issue Sep 15, 2016 · 6 comments

Comments

@xseano
Copy link

xseano commented Sep 15, 2016

Hi again Typicode, xD

I was a bit confused about accessing deep properties in your documentation. My question was, how would I access an array within the main object?

For example, here is my db.json:

{ "users": [ { "id": "hhhhadjklsajkdlaskjd", "unm": "sean", "email": "sean.o@mac.com", "pwd": "black", "todos_list1": [ { "id": 4, "title": "asd", "descrp": "asd" } ], "todos_list2": [ { "id": 1, "title": "Book", "descrp": "Read Outliers" }, { "id": 2, "title": "sadasd", "descrp": "Reassasdasdasdssliers" } ], "todos_yo": [

In which I can access the main object under http://localhost:3000/users/hhhhadjklsajkdlaskjd , but since POST with arrays is not supported, how would I be able to access an array like "todos_list1" and view what objects are stored inside this array?

Ex (When I can view todos_list1 specifically, I would see this):
{ "id": 4, "title": "asd", "descrp": "asd" }

Please let me know, or if there is another way to POST objects to an array.

Thanks!

@xseano xseano changed the title Deep Properties Accessing Nested Values Sep 15, 2016
@typicode
Copy link
Owner

Hi @xseano,

There's no way to just "pick" todos_list1 with this db.json structure.

If possible I would recommend this kind of db.json:

{  
   "users":[  
      {  
         "id":1,
         "unm":"sean"
      }
   ],
   "todos":[  
      {  
         "id":1,
         "name":"list1",
         "items":[  
            {  
               "title":"asd",
               "descrp":"asd"
            }
         ],
         "userId":1
      },
     {  
         "id":2,
         "name":"list2",
         "items":[  
            {  
               "title":"Book",
               "descrp":"Read Outliers"
            },
            {  
               "title":"sadasd",
               "descrp":"Reassasdasdasdssliers"
            }
         ],
         "userId":1
      }
   ]
}

This way you can do:

GET /users/1?_embed=todos # get user 1 + todos
GET /users/1/todos # get all todos for user 1
GET /users/1/todos?name=list1 # get todo for list 1

To create a new todo list, you now have a dedicated URL:

POST /users/1/todos # payload: { name: 'list3', items: [] }

To update a todo list (all the items will be replaced by the payload, but depending of the size of it, it can be acceptable):

PUT /todos/1 # payload { name: 'list1', items: [ { title: "new item" } ] }

You may want to have some even finer grained URLs:

{
  users: []
  lists: [
    { id: 1, userId: 1 }
  ],
  items: [
    { id: 1, listId: 1 }
  ]
}

Then you would get:

/users/1
/users/1/lists
/lists/1
/lists/1/items

For inspiration, you can have a look at the doc, posts with comments are bit like users and todos.
And you can also experiment with requests live on https://github.com/typicode/jsonplaceholder

Hope it helps.

@xseano
Copy link
Author

xseano commented Sep 15, 2016

WOW, this definitely helps -- thank you so much!!! 👍

@SquealerAD
Copy link

if i understand correctly this -> POST /users/1/todos # payload: { name: 'list3', items: [] } will update all todos objects in the array no metter what is userId. how can i update specific todo list for specific user?

@virgil-av
Copy link

posting POST /users/1/todos this will error out and not actually post in /users

@fatemehqasemkhani
Copy link

@typicode Saved my day, thanks

@archana-ragam
Copy link

How to access the nested data from json data using json-server.
This is my json data
{
"flightDetails": [
{
"id": 1,
"flight_name": "INDIGO",
"flight_departs": "8:00",
"flight_arrives": "9:00",
"flight_from": "New",
"flight_to": "Hyderabad",
"flight_date": "13/6/2023",
"flight_img": "/images/airindia.jpg",
"flight_classes": [
"Business",
"Premium economy",
"first",
"economy"
],

"services": [
{
"id": 2,
"service_name": "seat allocation",
"types": [
{
"Seat": "Extra Leg Room",
"cost": "rs/- 1500"
},
{
"Seat": "Front of Aircraft",
"cost": "rs/- 1000"
}
]
},
{
"id": 1,
"service_name": "Baggage",
"types": [
{
"Baggage_type": "Overweight",
"Baggage_limit": "0-3kgs",
"Baggage_cost_1kg": "rs/-200"
},
{
"Baggage_type": "Checked Bags ",
"Baggage_limit": "15kgs",
"Baggage_cost_1kg": "rs/-390"
}
]
},
{
"id": 3,
"service_name": "Baggage",
"types": [
{
"Baggage_type": "Overweight",
"Baggage_limit": "0-3kgs",
"Baggage_cost_1kg": "rs/-200"
},
{
"Baggage_type": "Checked Bags ",
"Baggage_limit": "15kgs",
"Baggage_cost_1kg": "rs/-390"
}
]
}
],
"passenger": [
{
"id": 1,
"passenger_seat_number": "20A",
"passenger_name": "RAGAM ARCHANA",
"passenger_address": "Hyd,Telangana",
"passenger_passport_details": {
"Citizen": "India",
"place": "Hyd,Telangana",
"date_of_birth": "03-08-2000",
"expire_date": "24/07",
"gender": "F"
}
}
]
}
]
}

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

6 participants