Parse or view JSON data fields using JQ tool utility where field names have a “-” dash in the key name

前端 未结 4 615
既然无缘
既然无缘 2020-12-12 08:05

I have a JSON data file (as shown below) and I\'m trying to find field values using jq utility.

It\'s working fine except for fields if the key name contains a

4条回答
  •  猫巷女王i
    2020-12-12 08:48

    Hm..took some time but finally it seems like we need to DOUBLE QUOTE it and back slash just the double quote for any key name containing a - in it's name.

    $ cat /tmp/my.data.json| jq ".content.book1.\"field-2\""                    
    "value-2"
    
    $ cat /tmp/my.data.json| jq ".content.book1.\"field-three\".\"url\""  
    "book1/field-three/"
    

    OR if you wrap everything in a single quotes ', then we do NOT need to backslash " double quotes but use double quotes for key names with - in their name.

    $ cat /tmp/my.data.json| jq '.content.book1."field-three"."url"'  
    "book1/field-three/"
    

    Hope it helps! Took some help/hint from https://jqplay.org/

    See this for more: https://github.com/stedolan/jq/issues/38#issuecomment-9770240

提交回复
热议问题