簡體   English   中英

使用 jq 解析 json 個值

[英]Parsing json values using jq

我正在嘗試在 linux 命令行上使用 jq 獲取 JSON 結構的值“ en ”。

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

我有超過 5000 個文件,從0001.json 0002.json.. 5000.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" }
}

我得到這個結果:

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 在所有 json 文件結果的一個文件中。

文件名&enValue:confidenceValue

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

你想要的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