Filter on children level, return the parent node
  • 09-May-2023
Lightrun Team
Author Lightrun Team
Share
Filter on children level, return the parent node

Filter on children level, return the parent node

Lightrun Team
Lightrun Team
09-May-2023

Explanation of the problem

The problem at hand involves filtering on complex child objects and retrieving their parent nodes that match the criteria. Using the bookstore example from Jayway’s Github project, suppose we change the “author” field to a more complex object, such as an array of objects, each with a “firstName” and “lastName” field. The goal is to retrieve all books that were written by an author with the last name “Waugh.”

One attempt to achieve this is by using the Json Evaluator tool (http://jsonpath.herokuapp.com/) with a filter like $.store.book[?(@.authors[?(@.lastName == ‘Waugh’)])]. However, this approach does not seem to work, indicating that Jayway’s library may not support nested predicates. As a result, it is unclear how to proceed with filtering on complex child objects.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Filter on children level, return the parent node

Using the “jsonpath-plus” library to solve the problem of filtering on child level complex objects. The suggested version is “^0.16.0”. With this library, it is possible to search and retrieve the parent nodes whose children match the predicates.

The workaround involves using the expression “$..[?(@.firstName==”A”)]^title”. This expression enables you to search for all items with “firstName” equal to “A” and retrieve their parent node’s “title” property. This approach can be helpful when you have complex objects with nested arrays and need to filter and retrieve data based on certain criteria.

The limitation with the Jayway library is that it does not allow nested predicates, but the workaround with the “jsonpath-plus” library enables searching and retrieving parent nodes using complex objects. This approach can be useful for those who want to filter and retrieve data based on specific criteria.

 

Other popular problems with JsonPath

Problem: One of the most popular problems with JsonPath is related to handling arrays

When working with arrays in JsonPath, it can be challenging to identify the correct index of an element, especially if the index is not known in advance. For example, imagine you have the following JSON object:

{
    "fruits": [
        {
            "name": "apple",
            "color": "red"
        },
        {
            "name": "banana",
            "color": "yellow"
        }
    ]
}

Solution:

If you want to access the color of the first fruit in the list, you could use the following JsonPath expression: $.fruits[0].color. However, what if you don’t know the index of the fruit you want to access? In this case, you can use the * wildcard to match any index, like this: $.fruits[*].color. This expression will return an array of all the colors of the fruits.

Problem: filtering nested objects

JsonPath allows you to filter objects using the [] notation, but this can become more complicated when the object contains nested objects. For example, consider the following JSON:

{
    "person": {
        "name": "John",
        "address": {
            "city": "New York",
            "state": "NY"
        }
    }
}

Solution:

If you want to filter on the city property, you can use the following JsonPath expression: $.person.address[?(@.city == 'New York')]. This expression will return the entire address object for the person who lives in New York. The ? symbol is used to indicate a filter, and the @ symbol refers to the current object being filtered. The result of this expression will be:

{
    "city": "New York",
    "state": "NY"
}

Problem: accessing properties with special characters

For example, imagine you have the following JSON object:

{
    "my.property": "value"
}

Solution:

If you want to access the value of the my.property property, you might be tempted to use the following JsonPath expression: $.my.property. However, this expression will not work, because the dot notation is interpreted as a property separator. To access properties with special characters, you need to use the bracket notation, like this: $['my.property']. This expression will correctly return the value of the my.property property.

 

A brief introduction to Json Path

JsonPath is a query language used to extract data from JSON documents. It is similar to XPath in XML but with a different syntax. JsonPath allows developers to extract data from JSON documents by defining a path expression. The path expression is evaluated against the JSON document, and the resulting values are returned. The path expression is composed of one or more path elements, separated by a dot “.” character. Each path element can specify a filter, an index, or a wildcard.

JsonPath has become a popular tool for data extraction in web applications, especially those using RESTful APIs that return JSON responses. It is a lightweight and efficient way to extract specific data from JSON documents, which can be especially useful when dealing with large datasets. Developers can use JsonPath to extract specific data, such as a particular property or a set of properties, without having to retrieve the entire JSON document. This can result in faster processing times and improved performance. Additionally, JsonPath is supported by a variety of programming languages, including Java, JavaScript, Python, and Ruby, making it widely accessible to developers.

 

Most popular use cases for Json Path

  1. Data extraction: JsonPath is commonly used for data extraction from JSON data. It allows you to specify a path to the data you want to extract, using a similar syntax to XPath for XML. Here’s an example of how you might extract a value from a JSON object using JsonPath:
const data = {
  "name": "John Smith",
  "email": "john.smith@example.com",
  "age": 32
};

const name = jsonPath(data, "$.name");
console.log(name); // outputs "John Smith"
  1. Querying JSON: You can also use JsonPath to query JSON data, similar to SQL queries. This can be useful for filtering data, selecting specific fields, and more. Here’s an example of how you might use JsonPath to query a JSON array:
const data = [
  {
    "name": "John Smith",
    "email": "john.smith@example.com",
    "age": 32
  },
  {
    "name": "Jane Doe",
    "email": "jane.doe@example.com",
    "age": 28
  }
];

const result = jsonPath(data, "$[?(@.age > 30)]");
console.log(result); // outputs [{ name: "John Smith", email: "john.smith@example.com", age: 32 }]


  1. Validating JSON: JsonPath can also be used for JSON validation, allowing you to check that JSON data meets certain criteria. For example, you might use JsonPath to ensure that a JSON object has a certain property, or that a JSON array contains a certain value. Here’s an example of how you might use JsonPath to validate a JSON object:
const data = {
  "name": "John Smith",
  "email": "john.smith@example.com",
  "age": 32
};

const isValid = jsonPath(data, "$.name && $.email && $.age");
console.log(isValid); // outputs true
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.