Json-server: Nested request without any ids

Created on 11 Apr 2017  路  3Comments  路  Source: typicode/json-server

Hey guys,
I just start using json-server and struggling with one thing. I want to have URL which are nested so e.g. to get user orgs, request would looks like:
/rest/user/orgs and will return array of user orgs

    {
        "rest": {
            "user": {
                "select": {
                    "org": []
                },
                "orgs": [{
                    "id": "5601e1c0-317c-4af8-9731-a1863f677e85",
                    "name": "DummyOrg"
                }],
                "logout": {}
            }
        }
    }

Json as above is throwing 404. So any idea what I am doing wrong?

Most helpful comment

Hello,
I would like to ask, if is it possible get two relations, for example:

{
  "building": [
    {
      "id": "H",
      "name": "Home"
    }
  ],
  "room": [
    {
      "id": "112",
      "buildingId": "H",
      "name": "112"
    }
  ],
  "funktion": [
    {
      "id": 1,
      "roomId": "112",
      "ga": "2/1/20",
      "DPT": "DPT1",
      "commit": "Hello"
    }
  ]
}
  1. like this it is works:
    /building/H/room

  2. but following doesn't work:
    /building/H/room/112/funktion

it is possible to get a solution for this relation?

Br
Jakob

All 3 comments

Hey @melnikovic

Nested requests to access nested resources aren't supported. At least, not with this data structure.

I would suggest flattening db.json and prefix your requests with a custom route.

    {
            "select": {
                "org": []
            },
            "orgs": [{
                "id": "5601e1c0-317c-4af8-9731-a1863f677e85",
                "name": "DummyOrg"
            }],
            "logout": {}
    }

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

{
  "/rest/user/": "/"
}

Then you'll be able to access each resources

GET /rest/user/select
GET /rest/user/orgs
GET /rest/user/orgs/:id
...

Hello,
I would like to ask, if is it possible get two relations, for example:

{
  "building": [
    {
      "id": "H",
      "name": "Home"
    }
  ],
  "room": [
    {
      "id": "112",
      "buildingId": "H",
      "name": "112"
    }
  ],
  "funktion": [
    {
      "id": 1,
      "roomId": "112",
      "ga": "2/1/20",
      "DPT": "DPT1",
      "commit": "Hello"
    }
  ]
}
  1. like this it is works:
    /building/H/room

  2. but following doesn't work:
    /building/H/room/112/funktion

it is possible to get a solution for this relation?

Br
Jakob

@typicode Will you work on nested support in the future?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sboudouk picture sboudouk  路  3Comments

goldmont picture goldmont  路  3Comments

pantchox picture pantchox  路  3Comments

fishenal picture fishenal  路  3Comments

jasonlimantoro picture jasonlimantoro  路  4Comments