简体   繁体   中英

Extract value from JSON using json parser or regex

I have the following JSON:

{
    "c": {
        "21969": {
            "name": "TC",
            "status": "completed",
            "result": {
                "count": 421
            },
            "identifier": "column1"
        }
    },
    "analyses": {
        "1": {
            "name": "test",
            "status": "completed",
            "result": {
                "21969": {
                    "status": "completed",
                    "result": {
                        "columnDefs": [{
                            "headerName": "RD",
                            "field": "d_c"
                        }, {
                            "headerName": "LN",
                            "field": "l_n"
                        }, {
                            "headerName": "Count",
                            "field": "count"
                        }, {
                            "headerName": "% ",
                            "field": "percent"
                        },  {
                            "headerName": "os",
                            "field": "os"
                        }],
                        "rowData": [{
                            "d_c": "Total",
                            "line_number": 1,
                            "count": 869,
                            "sub_header": true
                        }, {
                            "d_c": "test01",
                            "l_n": "1",
                            "count": 356,
                            "percent": "40.97 %",
                            "os": "17.45"
                        }, {
                            "drug_combo": "test02",
                            "l_n": "2",
                            "count": 61,
                            "percent": "7.02 %",
                            "os": "29.75 (22.88 - 42.48)"
                        }
                         ]
                    }
                }
            }
        }
    }
    
}

I want to fetch the count values present only in these two blocks:

{
                            "d_c": "test01",
                            "l_n": "1",
                            "count": 356,
                            "percent": "40.97 %",
                            "os": "17.45"
                        }, {
                            "drug_combo": "test02",
                            "l_n": "2",
                            "count": 61,
                            "percent": "7.02 %",
                            "os": "29.75 (22.88 - 42.48)"
                        }]

For example I have tried $count.. but it gives all the count values eg 421,869,356,61 similarly for $l_n.. gives 1,1,2. Expected Result: - Want to fetch only 356 and 61 for count. For l_n only wants to fetch 1,1.

Can anyone help me on this?

Since your response contains more than one node named "count", you will have to be more precise.

In JMeter, the JSON Extractor allows you specify Match No. :

If the JSON Path query leads to many results, you can choose which one(s) to extract as Variables

Alternatively, something like $.analyses..rowData[1].count should give you the "count" value for "test01". Similarly $.analyses..rowData[2].count should give you the "count" value for "test02".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM