jq通过转换现有的json来创建新的json [英] jq create a new json by transforming an existing one

查看:178
本文介绍了jq通过转换现有的json来创建新的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    {
    "prodid_876006": {
        "serid": [{
            "seridone": "3265874"
        }, {
            "seridtwo": "21458915"
        }],
        "serials": ["028915"]
    },
    "prodid_980": {
        "serid": [{
            "seridone": "32743214"
        }, {
            "seridtwo": "5469872"
        }],
        "serials": ["192147","1632589"]
    }
}

所需的输出: 对于每个json对象,提取prodid_ info和serials数组,并使用以下格式创建一个新的json文件:

desired output: for each json object, extract the prodid_ info, and the serials array, and make a new json file, with the following format:

{    
"prodid_876006" : ["028915"],
"prodid_980" : ["192147","1632589"]
}

什么是jq命令?

keys ,.[].serials

提供以下信息:

[
  "prodid_876006",
  "prodid_980"
]
[
  "028915"
]
[
  "192147",
  "1632589"
]

更新的问题:

我还如何在另一个json中获取以下输出? (这里的键是serials数组的每个元素,值是第一个示例json的键):

how could i also get in another json the following output? (here the key is each element of the serials array, and the value is the key of the first sample json):

{    
"028915" : ["prodid_876006"],
"192147" : ["prodid_980"],
"1632589" : ["prodid_980"]
}

推荐答案

类似的方法将起作用,在此方法中,您提取键-值对数组并传送到from_entries:

Something like this will work, where you extract an array of key-value pairs and pipe to from_entries:

% jq '[to_entries[] | {"key": .key, "value": .value.serials}] | from_entries' 42762941.json
{
  "prodid_876006": [
    "028915"
  ],
  "prodid_980": [
    "192147",
    "1632589"
  ]
}

这篇关于jq通过转换现有的json来创建新的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆