← All Exercises

JSONPath Subset Query Engine

intermediatejavascript~30min

Background

JSONPath lets config tools and REST filters address deep values without hand-written traversal. This subset covers the large majority of real queries.

Problem

Implement a JSONPath subset supporting $.a.b style dot segments, [0] numeric indexes, and [*] wildcard expansion. Evaluate each path against the data document and output the resolved value; a wildcard path returns an array of all matches, a single match returns the scalar itself.

Input

A JSON object {"data": object, "paths": string[]}. Paths start with $, use dot segments and bracket indexes, and may contain one [*].

Output

A JSON object {"results": any[]} with one resolved value per path in order.

Sample tests

Sample 1

Input: {"data":{"a":{"b":[{"c":1},{"c":2}]},"items":[{"name":"x"},{"name":"y"}]},"paths":["$.a.b[1].c","$.items[*].name","$.missing"]}
Expected: {"results":[2,["x","y"],[]]}

Sample 2

Input: {"data":{"config":{"retries":3}},"paths":["$.config.retries"]}
Expected: {"results":[3]}

Your solution

Learning objective

Build a tokenizer plus stepwise evaluator for path-based data access.