按照jq编辑json文件

问题描述 投票:0回答:2

我有一个像这样的json文件:

{
"arrays": [
    {
      "name": "foo",
      "properties": [
        {
          "type": "app",
          "url": "https://example.com",
          "checksum": "d6fd580fe890b826250aa5d5a88bcdb28839cff4d447ba51536cda4ceae89c4e"
        }
      ]
    }
  ]
}

我想将类型更改为其他类型,删除 url 和校验和密钥,并在其位置添加文件密钥和值。

最后的事情应该是:

{
"arrays": [
    {
      "name": "foo",
      "properties": [
        {
          "type": "Bar",
          "file": "filename"
        }
      ]
    }
  ]
}

我将如何与

jq

内联执行此操作

我开始修改类型值:

jq '(.arrays[]| select(.name == "foo").properties[]).type |= "Bar"' test.json
json jq
2个回答
0
投票
.arrays[].properties |= map({ type: "FOOBAR", file: "someFile" })

将导致:

{
  "arrays": [
    {
      "name": "foo",
      "properties": [
        {
          "type": "FOOBAR",
          "file": "someFile"
        }
      ]
    }
  ]
}

在线尝试!

0
投票

我将替换整个属性字段:

jq '(.arrays[]| select(.name == "foo")).properties |= {
    "type" : "bar",
    "file" : "filename"
}' test.json

您可以在这里尝试一下

© www.soinside.com 2019 - 2024. All rights reserved.