user177800
user177800

Reputation:

JsonPath expression to filter on an attribute and select a partial record?

Here are the details about what I am using:

<dependency>
  <groupId>com.jayway.jsonpath</groupId>
  <artifactId>json-path</artifactId>
  <version>2.0.0</version>
</dependency>

I have the following example JSON.

[
    {
        "something": "who cares",
        "job_name":"my-long-job-name-that-ends-in-box",
        "some_number": 1
    },
    {
        "something": "who care here either",
        "job_name": "my-long-job-name-that-ends-in-something-else",
        "some_number": 2
    }
]

So far I can match a record that ends in box:

$.[?(@.job_name =~ /^.+box/i )]

finds all the objects that have job_name ending in box and dumps out the entire record. This is ok, but I would like to specify a subset of attributes to return instead of the entire object sometimes.

In my real data there are dozens of fields, I only really need to see a few of them at a time.

$.[?(@.job_name =~ /^.+box/i )].job_number will only return the job_number.

Using the example data above how would return job_name and some_number but not something?

How would I modify my JsonPath expression to return just the fields that I want to see.

Upvotes: 1

Views: 2485

Answers (1)

user177800
user177800

Reputation:

Solution

After some more trial and error with the documentation and one more thing I had not tried yet I found the solution:

$.[?(@.job_name =~ /^.+box/i )].['job_name','some_number']

will return a Map with only the job_name and some_numberas keys for only the one record in the example data.

Upvotes: 1

Related Questions