I am trying to get the values of name field where user_comment=<something I supply to the batch script>
{"snapshots":[{"name":"JqJiP","user_comment":"Clean_image","current":"n","created":"2020-03-14"},{"name":"N4bk7","user_comment":"Import","current":"n","created":"2020-03-16"},{"name":"jOtfa","user_comment":"Baseline","current":"y","created":"2020-03-20"}]}
{"snapshots":[{"name":"dcLD5","user_comment":"Clean_image","current":"n","created":"2020-03-14"},{"name":"tO8hN","user_comment":"Import","current":"n","created":"2020-03-16"},{"name":"yOgNY","user_comment":"Baseline","current":"y","created":"2020-03-20"}]}
For example if I want to get the value of "user_comment":"Clean_image"
The output should be
JqJiP
dcLD5
Thank you for the help
Batch doesn't understand JSON, so you'd have to use a tool that does. The JSON-parser xidel is such a tool.
xidel -s "input.json" -e "$json/(snapshots)()[user_comment='Clean_image']/name"
JqJiP
dcLD5
$json
: The file content parsed as JSON.(snapshots)()
: All members (objects in this case) within the snapshots
-arrays.[user_comment='Clean_image']
: Only those objects that have a user_comment
-attribute with the value "Clean_image"
.name
: The value of the name
-attribute within those objects.See also this online Xidel demo.