I would like to retrieve a value "tenki-deploy-topic" from the below JSON file content. I used this regex grep -o 'topic[^"]*' output.log
, then it gets all the files with the topic, instead, I want to use some other key such as "tenki" or "dense".
{ "Topics": [ { "Name": "arn:aws:sns:us-west-2:123456789012:topic-freezing-deploy" }, { "Name": "arn:aws:sns:us-west-2:123456789012:topic-dense-deploy" }, { "Name": "arn:aws:sns:us-west-2:123456789012:topic-tenki-deploy" } ] }
Best Answer
Using the typical CLI tools of Linux/Unix such as
awk
,sed
, &grep
are almost always the wrong way to go when trying to parse JSON.My typical goto tool for JSON CLI work at the terminal is
jq
. It's in all the repos and can be easily installed.Your problem of how to select just the elements in the
Topics
array becomes trivial:The above works by taking the elements from the
.Topics[]
array and then selecting the.Name
whose value containstopic-tenki-deploy
.References