I have this json text:
{
"buildStatus" : {
"status" : "ERROR",
"conditions" : [{
"status" : "OK",
"metricKey" : "bugs"
}, {
"status" : "ERROR",
"metricKey" : "test_success_density"
}, {
"status" : "OK",
"metricKey" : "vulnerabilities"
}
],
"periods" : []
}
}
I want to extract the overall status of the buildStatus, i.e the expected output was "ERROR"
"buildStatus" : {
"status" : "ERROR",
....
}
I tried the sed expression below, but it isn't working, it returns OK
:
status= sed -E 's/.*\"buildStatus\":.*\"status\":\"([^\"]*)\",.*/\1/' jsonfile
What am I doing wrong?
Best Answer
Do not parse complex nested data structures like JSON or XML with regular expressions, use a proper JSON parser, like
jshon
.First you need to install it:
Then you have to provide it the JSON data to parse via standard input, so you can either redirect another command's output there with a pipe (
|
) or redirect a file to it (< filename
).The arguments it needs to extract the data you want look like this:
-e "buildStatus"
picks the element with the "buildStatus" index from the top level dictionary.-e "status"
picks the element with the "status" index from the second level dictionary picked above.-u
converts the selected data from JSON to plain data (i.e. here it removes the quotes around the string)So the command you run, depending on where you get the data from, looks like one of those:
To learn more about
jshon
, you can read its manpage accessible online here or by simply typingman jshon
.