简体   繁体   English

使用 jq 解析 json 个值

[英]Parsing json values using jq

I am trying to get values " en " of a JSON structure using jq on the linux command line.我正在尝试在 linux 命令行上使用 jq 获取 JSON 结构的值“ en ”。

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我有超过 5000 个文件,从0001.json 0002.json.. 5000.json开始

This is a simple file 0001.json这是一个简单的文件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. My Desired Output 在所有 json 文件结果的一个文件中。

filename&enValue:confidenceValue文件名&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:你想要的jq过滤器可以这样写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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