JSON path basics

JSON path is a query language that can help you parse data represended in JSON format.

Example 1 - given JSON:

{
  "car": {
    "color": "blue",
    "price": "$20,000"
  },
  bus: {
    "color": "white",
    "price": "$120,000"
  }
}
$ - in JSON path denotes the root element

Get car details:
$.car
Result:
[
  {
    "color": "blue",
    "price": "$20,000"
  }
]
Get bus details:
$.bus
Result:
[
  {
    "color": "white",
     "price": "$120,000"
  }
]
Get car's color:
$.car.color
Result:
[
  "blue"
]
Get bus's price:
$.bus.price
Result:
[
  "$120,000"
]
Example 2 - given JSON:
[
  "car",
  "bus",
  "truck",
  "bike"
]
Get the 1st element:
$[0]
Result:
[
  "car"
]
Get the 4th element:
$[3]
Result:
[
  "bike"
]
Get the 1st and 4th elements:
$[0,3]
Result:
[
  "car",
  "bike"
]
Example 3 - given JSON:
{
  "car": {
    "color": "blue",
    "price": "$20,000",
    wheels: [
      {
        "model": "X345ERT",
        "location": "front-right"
      },
      {
        "model": "X346GRX",
        "location": "front-left"
      },
      {
        "model": "X236DEM",
        "location": "rear-right"
      },
      {
        "model": "X987XMV",
        "location": "rear-left"
      }
    ]
  }
}
Get the model of the 2nd wheel:
$.car.wheels[1].model
Result:
[
  "X346GRX"
]
Example 4 - given JSON:
[
  12,
  43,
  23,
  12,
  56,
  43,
  93,
  32,
  45,
  63,
  27,
  8,
  78
]
Get all numbers greater than 40:
$[?(@ > 40 )]
@ - each item in the list

Instead of @ > 40 could be: Result:
[
  43,
  56,
  43,
  93,
  45,
  63,
  78"
]
From Example 3 get the model of the rear-right wheel:
$.car.wheels[?(@.location == "rear-right")].model
Result:
[
  "X236DEM"
]