Request a specific variable of JSON stream using bash/terminal

Viewed 34

I am working with a flutter web server testing. I writing a simple bash script to fetch some JSON data from API request. The API request dispatch following information as JSON response.

{

    "code_version":{
        "engine_name":"flutter_renderV1",
        "proxy":"10.1.1.1:1090",
        "test_rate":true,
        "test_density":"0.1",
        "mapping_eng":"flutter_default_mapper"

    },
    "developer_info":{
        "developerid":"30242",
        "context":true,
        "request_timestamp":"156122441"

    }

}

Once this received, I saved in to local file named server_response{$id}.json. I need to collect test_density value under code_version data frame. I used several awk, sed command to fetch data, unfortunatly I cannot get the exact output from my terminal.

1 Answers

You need to install powerful JSON querying processor like jq processor. you can can easily install from here

once you install jq processor, try following command to extract the variable from JSON key value

suppose, your file named as server_response_123.json,

jq '.code_version.test_density' server_response_123.json

the output will be shown as,

"0.1"
Related