In a JSON pair {"string":"value"}, I am trying to return the string. What JSONPath query do I need to send? As an example, in the JSON below, I'd like my query to return the word "category" of the first line in the array.
Thanks!
Torben
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
the query is: $.store.book[:1].category
[:1] = The item on Position 1
[0:3] = The item on position 1,2,3
[2:4] = The item on position 3,4
Related
having the following book store I want to find all books that match the categories 'fiction' and 'fantasy'
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "adventure",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fantasy",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
I found this example [?(#.size in ['S', ‘M'])] here which looks exactly like what I want but I could not get it working at https://jsonpath.com
This is what I tried:
$.store.book.[?(#.category in ['fiction','fantasy'])]
Looking for just a single criteria value works at https://jsonpath.com:
$.store.book.[?(#.category == 'fiction')]
Thank you for any hint pointing me into the right direction.
Try using logical OR ( || symbol) for multiple criteria.
updated expression:
$.store.book[?(#.category == 'fiction' || #.category == 'fantasy')]
Output:
Reference:
https://docs.hevodata.com/sources/engg-analytics/streaming/rest-api/writing-jsonpath-expressions/#filters
Let's say I have this popular data sample.
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"color": "red",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"color": "blue",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
What I want from here is a all the book where category == fiction and all the bicycle where color == red.
That is, I want,
{
"category": "fiction",
"author": "Evelyn Waugh",
"color": "red",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"color": "blue",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
},
"bicycle": {
"color": "red",
"price": 19.95
}
I know, I can use $.store.book[?(#.category == "fiction")] to achieve the targeted books and $.store.bicycle[?(#.color == 'red')] to achieve the targeted bicycle.
But how can I achieve both at one go?
You can use && and || operators.
$.store.*[?(#.color== 'red' || #.category == 'fiction')]
will fetch the results as you wanted
I would recommend implementing your scenario using JSR223 PostProcessor and Groovy language. Example code to extract the elements and build a new JSON out of the filtered data will look something like:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def json = new JsonSlurper().parseText(prev.getResponseDataAsString())
def fictionBooks = json.store.book.findAll {it.category == "fiction"}
def redBikes = json.store.bicycle.findAll{it.color="red"}
JsonBuilder builder = new JsonBuilder(fictionBooks + redBikes)
prev.setResponseData(builder.toPrettyString())
The above code will replace the parent sampler response data with the filtered JSON
References:
Groovy: Parsing and Producing JSON
JsonBuilder class reference
Groovy Is the New Black
To return books and bicycle, both you need to :
$..*[?(#.color== 'red' || #.category == 'fiction')]
For books which have color==red or category==fiction, you will need :
$.store.*[?(#.color== 'red' || #.category == 'fiction')]
Given the JSON below:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century"
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3"
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": null,
"isbn": "0-395-19395-8",
"price": null
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
What JSON path could I use to retrieve the first element of book that contains a category, author, price, and title parameter? To be clear, I don't want any category, author, price, and title parameter -- only those that come from a single book object. Also, the structure of the book array varies (i.e. the number and configuration of its elements changes over time) so I can't really hardcode anything.
I think you mean you want:
$..book[?(#.category && #.author && #.price && #.title)]
Given the following JSON:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
We can use a path expression like $.store.book[*].author to get all the authors. But what if we don't know beforehand whether book is an array or an object, e.g. what if we could have the above JSON but also something like this:
{
"store": {
"book": {
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
Is there a "generic path" that could be used to get all authors in either case?
Yes, you could use:
$.store..author
Testable here:
https://jsonpath.curiousconcept.com/
'Hope this helps.
{
"store": {
"book": [ {
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}, {
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}, {
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}, {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
} ],
"bicycle": [ {
"color": "red",
"price": 19.95,
"style": [ "city", "hybrid" ]
}, {
"color": "blue",enter code here
"price": 59.91,
"style": [ "downhill", "freeride" ]
} ]
}
}
How to fetch the title of book having author name contains 'Rees' using json path, I have tried using "$.book.[?(#.author =~ /.*'Rees')]" but its not working for me, can anyone please help me.