hiaclibe hiaclibe - 1 year ago 65
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\":\"[^(\",)]*\""