Ahasanul Haque
Ahasanul Haque

Reputation: 11134

How to join two json path query in json path extractor of jmeter?

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?

Upvotes: 1

Views: 2562

Answers (3)

bit2
bit2

Reputation: 61

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')] 

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168072

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

JMeter Groovy JSON

References:

Upvotes: 0

vins
vins

Reputation: 15370

You can use && and || operators.

$.store.*[?(@.color== 'red' || @.category == 'fiction')]

will fetch the results as you wanted

Upvotes: 0

Related Questions