Just did an online screening with Tesla, they ask a variation on Basic Calculator 2/3

JSONPath Evaluator

Problem Description

Implement a function that evaluates JSONPath expressions to extract data from a JSON object. JSONPath is a query language for JSON, similar to how XPath is used for XML.

Your task is to implement the evalJq(json_data, expression) function that takes a JSON object and a JSONPath expression and returns the corresponding value(s) from the JSON data.

JSONPath Expression Syntax

Your implementation should support the following JSONPath features:

  1. $ - The root object/element
  2. .property - Child operator (access direct properties)
  3. [index] - Array index expressions to access array elements
  4. [start:end] - Array slice operator to get a range of elements

Examples

For the following JSON data:

{
  "traveler": "Alex Johnson",
  "passport": "AB123456",
  "mileagePoints": 75000,
  "homeAirport": {
    "code": "SFO",
    "name": "San Francisco International",
    "country": "USA"
  },
  "recentFlights": [
    {
      "flightNumber": "UA456",
      "from": "SFO",
      "to": "JFK",
      "date": "2025-03-15",
      "class": "business"
    },
    {
      "flightNumber": "LH789",
      "from": "JFK",
      "to": "FRA",
      "date": "2025-03-20",
      "class": "economy"
    },
    {
      "flightNumber": "BA321",
      "from": "FRA",
      "to": "LHR",
      "date": "2025-03-22",
      "class": "premium"
    }
  ]
}

The function should return:

- `evalJq(data, "$")` → The entire JSON object
- `evalJq(data, "$.traveler")` → "Alex Johnson"
- `evalJq(data, "$.homeAirport.code")` → "SFO"
- `evalJq(data, "$.mileagePoints")` → 75000
- `evalJq(data, "$.recentFlights[0].flightNumber")` → "UA456"
- `evalJq(data, "$.recentFlights[2].class")` → "premium"
- `evalJq(data, "$.recentFlights[:].to")` → ["JFK", "FRA", "LHR"]

Constraints

  • The input JSON data will be a valid JSON object.
  • The JSONPath expression will be a valid string following the syntax described above.
  • You must handle nested objects and arrays.
  • For array slice notation [start:end], if either start or end is missing, it should be treated as the start or end of the array respectively.

Approach

You need to parse the JSONPath expression and navigate through the JSON data structure accordingly. Consider implementing a recursive approach to handle nested structures and complex queries.

Your implementation should handle the edge cases demonstrated in the provided code, such as array indexing and array slicing with appropriate error handling for invalid paths.

Comments (0)