修改json中的字符串

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

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

{
"arrays": [
    "a_string",
    {
      "name": "foo",
      "properties": [
        {
          "type": "some_type1",
          "url": "https://example.com",
          "checksum": "d6fd580fe890b826250aa5d5a88bcdb28839cff4d447ba51536cda4ceae89c4e"
        }
      ]
    },
    "another_string",
    {
      "name": "bar",
      "properties": [
        {
          "type": "some_type2",
          "url": "https://example.org",
          "checksum": "d6fd580fe890b826250aa5d5a88bcdb28839cff4d447ba51536cda4ceae89c4e"
        }
      ]
    }
  ]
}

我想修改

"a_string"
来表示
"b_string"
以及
properties
foo
块,例如
jq '(.arrays[]| select(.name == "foo")).sources |= [{"type" : "sometypeB", "file" : "filename"}]'

但是

select(.name == "foo")
在这里失败,因为字符串没有任何
name
,知道如何修改两者吗?

最终的 json 应如下所示:

{
"arrays": [
    "b_string",
    {
      "name": "foo",
      "properties": [
        {
          "type": "sometypeB",
          "file": "filename"
        }
      ]
    },
    "another_string",
    {
      "name": "bar",
      "properties": [
        {
          "type": "some_type2",
          "url": "https://example.org",
          "checksum": "d6fd580fe890b826250aa5d5a88bcdb28839cff4d447ba51536cda4ceae89c4e"
        }
      ]
    }
  ]
}
json jq
© www.soinside.com 2019 - 2024. All rights reserved.