Skip to main content
Welcome to this comprehensive guide on using the JSONPath wildcard (*) operator. In this lesson, you’ll discover how to apply wildcards to both JSON objects and arrays, and how to combine them for powerful queries.
JSONPath implementations can differ in syntax and features. Always test your expressions with your chosen library or tool.

Wildcards in JSON Objects

Imagine you have a JSON document with two top-level entries, car and bus, each containing color and price properties:
{
  "car": {
    "color": "blue",
    "price": "$20,000"
  },
  "bus": {
    "color": "white",
    "price": "$120,000"
  }
}
TaskJSONPath ExpressionOutput
Get the car’s color$.car.color["blue"]
Get the bus’s color$.bus.color["white"]
Get all colors$.*.color["blue","white"]
Get all prices$.*.price["$20,000","$120,000"]
  • Expression: $.*.color
    Result: ["blue","white"]
  • Expression: $.*.price
    Result: ["$20,000","$120,000"]

Wildcards in JSON Arrays

Next, consider a simple list of wheel objects:
[
  { "model": "X345ERT", "location": "front-right" },
  { "model": "X346ERT", "location": "front-left"  },
  { "model": "X347ERT", "location": "rear-right"  },
  { "model": "X348ERT", "location": "rear-left"   }
]
TaskJSONPath ExpressionOutput
First wheel’s model$[0].model["X345ERT"]
Fourth wheel’s model$[3].model["X348ERT"]
All wheels’ models$[*].model["X345ERT","X346ERT","X347ERT","X348ERT"]
  • Expression: $[*].model
    Result: ["X345ERT","X346ERT","X347ERT","X348ERT"]

Combining Wildcards with Property Access

You can mix object wildcards and array wildcards for nested structures. Here’s a JSON object where each vehicle has a wheels array:
{
  "car": {
    "color": "blue",
    "price": 20000,
    "wheels": [
      { "model": "X345ERT" },
      { "model": "X346ERT" }
    ]
  },
  "bus": {
    "color": "white",
    "price": 120000,
    "wheels": [
      { "model": "Z227KLJ" },
      { "model": "Z226KLJ" }
    ]
  }
}
TaskJSONPath ExpressionOutput
Car’s 1st wheel model$.car.wheels[0].model["X345ERT"]
Car’s all wheel models$.car.wheels[*].model["X345ERT","X346ERT"]
Bus’s wheel models$.bus.wheels[*].model["Z227KLJ","Z226KLJ"]
All vehicles’ wheel models$.*.wheels[*].model["X345ERT","X346ERT","Z227KLJ","Z226KLJ"]
  • Expression: $.*.wheels[*].model
    Result: ["X345ERT","X346ERT","Z227KLJ","Z226KLJ"]

Tips for Building Complex JSONPath Queries

  1. Break your expression into smaller parts:
    • Test one branch (e.g., $.car.wheels[*].model).
    • Then test another (e.g., $.bus.wheels[*].model).
  2. Once each part works, combine them with wildcards:
    • Final: $.*.wheels[*].model
  3. Use online JSONPath evaluators to verify results interactively.
Overusing deep wildcards ($..*) can be inefficient on large JSON documents. Optimize your path when performance matters.

Further Reading and Tools


Practice creating your own JSONPath expressions with wildcards to master complex data extraction scenarios!