Saturday, October 29, 2022

[SOLVED] Bash grep JSON Response

Issue

Here is the JSON Response in a File, namely, RGLogin.json

{"response":{"session_id":"2d48cc11ceabf28c9e92f4b677337dcd"},"response_status":200,"response_details":null}

The grep command below can retrieve session_id

grep -m1 -oP '\s*"session_id"\s*:\s*"\K[^"]+' RGLogin.json

But the following grep command can not retrieve response_status

grep -m1  -oP '\s*"response_status"\s*:\s*"\K[^"]+' RGLogin.json

Solution

For session id, it is looking for quotes. For response status, it is not quoted.

grep -m1  -oP '\s*"response_status"\s*:\s*\K[^,]+' RGLogin.json


Answered By - Guru
Answer Checked By - Katrina (WPSolving Volunteer)