Json-server: Accessing Nested Values

Created on 15 Sep 2016  路  4Comments  路  Source: typicode/json-server

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": "[email protected]", "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!

Most helpful comment

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.

All 4 comments

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.

WOW, this definitely helps -- thank you so much!!! 馃憤

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?

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

Was this page helpful?
0 / 5 - 0 ratings