Skip to content

Instantly share code, notes, and snippets.

@amoslanka
Last active December 19, 2018 09:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amoslanka/5616353 to your computer and use it in GitHub Desktop.
Save amoslanka/5616353 to your computer and use it in GitHub Desktop.
JSON Root Nodes

Some json configurations will emit a single root node named after the object's type or an abstractable name for the object depending on context. This document outlines the differences and examples of both included and discluded root nodes in JSON responses.

Single Record Requests

Nodes of this type, for request GET foos will respond with this body:

{
  "foo": {
    "bar": "baz"
  }
} 

Responses that do not respond with a root node will, for request GET foos, respond with this body:

{
  "bar": "baz"
}

Responses involving nested resources will not emit those nested resources' root nodes, however:

{
  "bar": "baz",
  "herp": {
    "derp": "yerp"
  }
}

Collection Requests

Collection requests are configured similarly. Assuming that a root node name can be derived from classname (being the same for all items in the collection) or from an abstracted name (such as 'items'), a response body for request GET foos is formatted:

{
  "foos": [
    {
      "bar": "baz"
    },
    {
      "bar": "derp"
    }
  ]
}

For a response not providing a root node, a response body for request GET foos is formatted:

[
  {
    "bar": "baz"
  },
  {
    "bar": "derp"
  }
]
@pooanm
Copy link

pooanm commented Jun 3, 2016

Hi,

i want json response like this format
"foos": [
{
"bar": "baz"
},
{
"bar": "derp"
}
]
}

but i don't kown...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment