jsonbatch-file

Parse json output in batch


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


Solution

  • Batch doesn't understand JSON, so you'd have to use a tool that does. The JSON-parser is such a tool.

    xidel -s "input.json" -e "$json/(snapshots)()[user_comment='Clean_image']/name"
    JqJiP
    dcLD5
    

    See also this online Xidel demo.