Hone logo
Hone
Problems

Implement JSON Path Query in JavaScript

You're tasked with building a simplified JSON path query engine in JavaScript. This engine will allow users to extract specific data from nested JSON objects using a string-based path notation. This is a fundamental operation for data manipulation and retrieval in many applications.

Problem Description

Implement a JavaScript function queryJsonPath(json, path) that takes two arguments:

  1. json: A JavaScript object representing the JSON data.
  2. path: A string representing the JSON path query.

The function should return the value found at the specified path within the json object. The path will use dot notation for object properties and bracket notation for array indices.

Key Requirements:

  • Object Property Access: Use dot (.) to access properties of JavaScript objects (e.g., user.name).
  • Array Element Access: Use bracket notation ([]) with an integer index to access elements of JavaScript arrays (e.g., items[0]).
  • Nested Access: Support deep nesting of objects and arrays (e.g., data.users[1].address.city).
  • Handling Non-existent Paths: If any part of the path does not exist, the function should return undefined.
  • Root Access: The path can start directly with a property name (e.g., name) or an array index if the root is an array.

Expected Behavior:

  • Successfully traverse the json object according to the path.
  • Return the final value at the end of the path.
  • Return undefined if the path is invalid or leads to a non-existent location.

Edge Cases to Consider:

  • Empty JSON object or array.
  • Empty path string.
  • Path segments that are numbers but refer to object properties.
  • Path segments that are strings but refer to array indices (these should be treated as invalid).
  • Arrays containing undefined or null values.

Examples

Example 1:

Input:
json = {
  "user": {
    "name": "Alice",
    "age": 30,
    "address": {
      "street": "123 Main St",
      "city": "Anytown"
    }
  },
  "isActive": true
}
path = "user.name"

Output: "Alice"
Explanation: We access the "user" object, then its "name" property.

Example 2:

Input:
json = {
  "items": [
    {"id": 1, "name": "Apple"},
    {"id": 2, "name": "Banana"},
    {"id": 3, "name": "Cherry"}
  ]
}
path = "items[1].name"

Output: "Banana"
Explanation: We access the "items" array, then the element at index 1, and finally its "name" property.

Example 3:

Input:
json = {
  "config": {
    "settings": [
      "optionA",
      null,
      "optionC"
    ]
  }
}
path = "config.settings[1]"

Output: null
Explanation: We access the "config" object, then "settings" array, and retrieve the element at index 1, which is null.

Example 4:

Input:
json = {
  "data": {
    "users": [
      {"id": 1, "name": "Bob"},
      {"id": 2, "name": "Charlie"}
    ]
  }
}
path = "data.users[5].name"

Output: undefined
Explanation: The path attempts to access an index (5) that is out of bounds for the "users" array.

Example 5:

Input:
json = {
  "person": {
    "age": 25
  }
}
path = "person.address.city"

Output: undefined
Explanation: The path attempts to access "address" within "person", but "address" does not exist.

Constraints

  • The json argument will always be a valid JavaScript object or array.
  • The path argument will always be a string.
  • The maximum depth of nesting in the JSON object will not exceed 50.
  • The maximum length of the path string will not exceed 200 characters.
  • Array indices in the path will be non-negative integers.

Notes

  • You'll need to parse the path string to extract the individual property names and array indices. Regular expressions can be very helpful here.
  • Consider how to handle a path that starts with an array index if the root json is an array.
  • Be mindful of type checking when accessing properties vs. array elements.
  • Think about how to iteratively traverse the json object based on the parsed path segments.
Loading editor...
javascript