JSON-SERVER

Nested Paths & ID Matching

Traverse into objects and arrays using URL path segments.

Object Traversal

Path segments resolve to object keys:

/<username>/<filename>/key1/key2

Given this JSON at /johndoe/store:

{
  "name": "My Store",
  "inventory": {
    "items": [
      { "id": 1, "name": "Widget", "price": 9.99 },
      { "id": 2, "name": "Gadget", "price": 14.99 }
    ]
  }
}
GET
/johndoe/store
const res = await fetch("https://json.shahriyar.dev/johndoe/store")
const data = await res.json()
console.log(data)
GET
/johndoe/store/name
const res = await fetch("https://json.shahriyar.dev/johndoe/store/name")
const data = await res.json()
console.log(data)
GET
/johndoe/store/inventory/items
const res = await fetch("https://json.shahriyar.dev/johndoe/store/inventory/items")
const data = await res.json()
console.log(data)
GET
/johndoe/store/inventory/items/0
const res = await fetch("https://json.shahriyar.dev/johndoe/store/inventory/items/0")
const data = await res.json()
console.log(data)

Array Index vs ID Match

When a path segment reaches an array:

  • Numeric — treated as array index (0 = first element)
  • Non-numeric — matched against element id or _id
GET
/johndoe/products/abc-123
const res = await fetch("https://json.shahriyar.dev/johndoe/products/abc-123")
const data = await res.json()
console.log(data)

Finds element where id or _id equals "abc-123". Returns 404 if not found.

Primitive arrays (strings, numbers) only support numeric index.

ID Match in Mutations

POST, PATCH, DELETE use last path segment as ID when targeting array:

PATCH
/johndoe/products/5
const res = await fetch("https://json.shahriyar.dev/johndoe/products/5", {
  method: "PATCH",
  headers: { Authorization: "Bearer js_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" },
  body: JSON.stringify({...}),
})
const data = await res.json()
console.log(data)
DELETE
/johndoe/products/5
const res = await fetch("https://json.shahriyar.dev/johndoe/products/5", {
  method: "DELETE",
  headers: { Authorization: "Bearer js_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" },
})
const data = await res.json()
console.log(data)

Traverses to products array, finds element with id/_id = 5, applies mutation.

Nested ID

PATCH
/johndoe/store/inventory/items/2
const res = await fetch("https://json.shahriyar.dev/johndoe/store/inventory/items/2", {
  method: "PATCH",
  headers: { Authorization: "Bearer js_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" },
  body: JSON.stringify({...}),
})
const data = await res.json()
console.log(data)

Traverses to inventory.items array, matches ID 2, patches.

Batch Mutations

PATCH and DELETE accept query params for batch operations on nested arrays:

DELETE
/johndoe/store/inventory/items?inStock=false
const res = await fetch("https://json.shahriyar.dev/johndoe/store/inventory/items?inStock=false", {
  method: "DELETE",
  headers: { Authorization: "Bearer js_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" },
})
const data = await res.json()
console.log(data)

See Query Parameters.