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.
Your implementation should support the following JSONPath features:
$ - The root object/element.property - Child operator (access direct properties)[index] - Array index expressions to access array elements[start:end] - Array slice operator to get a range of elementsFor 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"]
[start:end], if either start or end is missing, it should be treated as the start or end of the array respectively.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.