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)]
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
Below is the server response in a JSON format which I need to work on, In this JSON some of the objects I need to pass in the next request, which I am successfully able to do for the first occurrence, but the problem is coming on randomization in the below JSON
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sword of Honour",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "test_title",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Sword of Honour",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "India",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
if I apply $..book[0][?(#.title == 'Sword of Honour')].author condition I am seeing successful output but when I use $..book[1][?(#.title == 'Sword of Honour')].author I get a blank O/P which I understand all because at book[1] level there is not title like that.
How do I randomize the data in every Iteration so that it picks different authors for the same title? we have to take multiple values from JSON and pass it in the next request.
Extract all the titles using JSON Extractor:
It will give you the following variables:
title_1=Sword of Honour
title_2=test_title
title_3=Sword of Honour
title_4=India
title_matchNr=4
Then you can use __Random() and __V() functions combination to refer the random book title where required:
${__V(title_${__Random(1,${title_matchNr},)},)}
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
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 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.