简体   繁体   中英

Parsing json values using jq

I am trying to get values " en " of a JSON structure using jq on the linux command line.

find . -name "*.json" -exec jq -r \ '(input_filename | gsub("^\\./|\\.json$";"")) as $fname (map(.tags) | .[] | .[] | .tag.en ) as $tags | "\($fname)&\($tags)"' '{}' + 

i have more than 5000 files, start from 0001.json 0002.json.. 5000.json

This is a simple file 0001.json

{
"result": {
    "tags": [
        { "confidence": 100, "tag": { "en": "turbine" } },
        { "confidence": 64.8014373779297, "tag": { "en": "wind" } },
        { "confidence": 63.3033409118652, "tag": { "en": "generator" } },
        { "confidence": 7.27894926071167, "tag": { "en": "device" } },
        { "confidence": 7.01708889007568, "tag": { "en": "line" } }
    ]
},
"status": { "text": "", "type": "success" }
}

i get this result:

0001&turbine
0001&wind
0001&generator
0001&device
0001&line
jq: error (at ./0001.json:0): Cannot iterate over null (null)
Ouptut..
jq: error (at ./0002.json:0): Cannot iterate over null (null)
Output..
jq: error (at ./0003.json:0): Cannot iterate over null (null)

My Desired Output in one file from all json files results.

filename&enValue:confidenceValue

0001&turbine:100,wind:64,generator:63,device:7,line:7
0002&...
0003&...
0004&...

The jq filter you want can be written as follows:

(input_filename | gsub("^\\./|\\.json$";"")) as $fname
| ( [ .result.tags[] | [.tag.en, (.confidence | floor)] | join(":") ]
    | join(",") ) as $tags
| "\($fname)&\($tags)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM