Jmeter - Json Path extractor - conditional selection - json

I'm trying to conditionally extract the value of an element from the following sample json response:
{
"books": [
{
"title": "book 1 title",
"author": {
"firstName": "author01",
"lastName": "abc"
}
}
{
"title": "book 2 title",
"author": {
"firstName": "author02",
"lastName": "xyz"
}
}
]
}
I want to select the book title where lastName == xyz. Here the expression that I use:
$.books[?(#.author.lastName=='xyz')]
but it returns []
Thanks

Your JSON Path expression is correct, demo:
however there is a problem with your JSON:
{
"books": [
{
"title": "book 1 title",
"author": {
"firstName": "author01",
"lastName": "abc"
}
}, <-----------------------------you need the comma here
{
"title": "book 2 title",
"author": {
"firstName": "author02",
"lastName": "xyz"
}
}
]
}
If your response cannot be resolved to a valid JSON - you will have to use Regular Expression Extractor instead. If it is - double check your JSON Path expression and also worth checking out JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

Try below expression:
$.books[?(#.author.lastName=='xyz')].title

Related

Jmeter - Using JSON Extractor for getting variables with the same name

This is my JSON:
[
{
"rooms": [
{
"name": "Name1",
"id": 148,
"isActive": true,
"properties": {
}
},
{
"name": "Name 2",
"id": 149,
"isActive": true,
"properties": {
}
},
{
"name": "Name 3",
"id": 150,
"isActive": true,
"properties": {
}
}
],
"timezone": "America\/New_York",
"name": "AnotherName",
"id": 88,
"isActive": false,
"properties": {
}
}]
In order to extract the "timezone" field I use: "$..timezone" and in return I get:
Result[0]=America/New_York
Using "$..id" will return:
Result[0]=88
Result[1]=148
Result[2]=149
Result[3]=150
The question is: Which syntax to use in order to extract only Result[0]=88 ?
I've been trying several available options but with no success.. Any ideas?
You're using .. operator which means deep scan so JMeter finds all id attributes and returns all their values.
If you want only the top-level ID for the first entry in the response JSON Array you need to explicitly set path to it like:
$.[0].id
if you need to get the id where timezone is America/New_York - the relevant JSON Path query would be:
$..[?(#.timezone == 'America/New_York')].id
More information: JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios
You can use $.[*]id to get Result[0]=88

Sending the JSON response as array or normal object

I am implementing a restful service where I am getting the pdf names and their ids from the database in the JSON fromat. Which one of the both the convenient JSON resful service response?
First Option:
{
"results": {
"documentNames": [
"test.pdf",
"ireport-ultimate-guide.pdf",
"sending report.pdf",
"Motor Hour.pdf"
],
"documentds": [
21116,
21117,
21118,
21119
]
}
}
Second Option:
{
"results": {
"21116": "test.pdf",
"21117": "ireport-ultimate-guide.pdf",
"21118": "sending report.pdf",
"21119": "Motor Hour.pdf"
}
}
I would use this "third option": The result is a list of object.
{
"result": [{
"id": "21116",
"filename": "test.pdf"
},
{
"id": "21117",
"filename": "ireport-ultimate-guide.pdf"
},
{
"id": "21118",
"filename": "sending report.pdf"
},
{
"id": "21119",
"filename": "Motor Hour.pdf"
}
]
}
because it better models the object structure.
I would create an entity for each document that contains both name and I'd.
[
{"name": "doc_1", "id": 123},
{"name": "doc_2", "id": 456}
]

json request into string

I have the following json structure:
{
"data": [
{
"number": 123,
"animal": "mush"
},
{
"number": "123",
"animal": ""
}
],
"animal_id": 1
}
How can I save it as a string?
It varies by language, but in JavaScript (which might be likely used in your case), JSON.stringify does this job.

JSON array returns syntax error

I'm trying to insert an array of strings in JSON but it isn't working.
json:
{
"1": [{
"id": "1",
"title": "test 1",
"galery": { "http://placehold.it/540x540" , "http://placehold.it/540x520"}
}]
}
It's returning me syntax error on "galery" line but I can't figure out what is it
For an array, use:
"galery": ["http://placehold.it/540x540", "http://placehold.it/540x520"]
For named properties, use something like:
"galery": { url1:"http://placehold.it/540x540", url2:"http://placehold.it/540x520" }
Use [ ] for array of images in gallery. { } will expect object (key/value pairs).
You can check valid JSON syntax at http://www.json.org/
Replace the braces with brackets.
{
"1": [{
"id": "1",
"title": "test 1",
"galery": [ "http://placehold.it/540x540" , "http://placehold.it/540x520" ]
}]
}
You have incorrect syntax at two places. Below is the correct one:
{
"1": {
"id": "1",
"title": "test 1",
"galery": ["http://placehold.it/540x540" , "http://placehold.it/540x520"]
}
}

How to use a JSON data source with assemble.io?

The assemble.io documentation gives some examples of using simple JSON as a data source, such as:
{
"title": "Assemble" ,
"author": "Brian Woodward"
}
But, what if I wanted to use a more complex JSON structure for my data (to display a list of books on a single page)? How can that be done?
{
"books": [
{
"title": "Book A",
"author": "Fred"
},
{
"title": "Book 47",
"author": "Joe"
}
]
}
Let say your library.json contain:
{
"books": [
{
"title": "Book A",
"author": "Fred"
},
{
"title": "Book 47",
"author": "Joe"
}
]
}
Use {{each}} (Assemble use handlebarjs for default templating) to display a list of books on a single page:
{{#each library.books}}
{{title}} - {{author}}
{{/each}}