hiaclibe hiaclibe -4 years ago 198
JSON Question

Bash function: extraxt different fields from json with grep regex without using jq

How can I write a bash function to extract a user defined field from json?

function getFieldValueFromJson {
key=$(echo $JSON |grep -Po $regex)
echo $key
getFieldValueFromJson field1 $JSON
getFieldValueFromJson field2 $JSON

Output is:



Any idea how to solve this problem, if JSON is never pritty printed?

Info: Ok I can use jq but I want to solve this just with grep & regex.

UPDATE: Now I'm using jq due to several problems. Please upvote my question, so others do not use grep for json parsing!

Answer Source

With regex="\"$JSON_FIELD\":\"[^(\",)]*\""


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download