替换Json中的值

时间:2017-09-06 13:32:26

标签: json bash jq

我有以下JSON数据:

[
  {
    "item": {
      "name": "tso",
      "type": "table",
      "displayName": "TSO",
      "operators": [
        "in",
        "not in",
        "=",
        "<>"
      ],
      "values": []
    },
    "operator": "in",
    "value": [
      "NL"
    ],
    "conjunction": " ",
    "level": 0,
    "userFriendlyValue": [
      "TENNET_NL"
    ]
  },
  {
    "item": {
      "name": "elementType",
      "type": "select",
      "displayName": "Element type",
      "operators": [
        "=",
        "<>"
      ],
      "values": [
        "line",
        "trafo"
      ]
    },
    "operator": "=",
    "value": "line",
    "conjunction": "and",
    "level": 1,
    "userFriendlyValue": null
  },
  {
    "item": {
      "name": "optionalName",
      "type": "table",
      "displayName": "Element optional name",
      "operators": [
        "in",
        "not in",
        "=",
        "<>",
        "like"
      ],
      "values": []
    },
    "operator": "like",
    "value": "*110*",
    "conjunction": "and",
    "level": 2,
    "userFriendlyValue": null
  },
  {
    "item": {
      "name": "optionalName",
      "type": "table",
      "displayName": "Element optional name",
      "operators": [
        "in",
        "not in",
        "=",
        "<>",
        "like"
      ],
      "values": []
    },
    "operator": "like",
    "value": "*150*",
    "conjunction": "or",
    "level": 2,
    "userFriendlyValue": null
  },
  {
    "item": {
      "name": "optionalName",
      "type": "table",
      "displayName": "Element optional name",
      "operators": [
        "in",
        "not in",
        "=",
        "<>",
        "like"
      ],
      "values": []
    },
    "operator": "like",
    "value": "KV_VS",
    "conjunction": "or",
    "level": 2,
    "userFriendlyValue": null
  },
  {
    "item": {
      "name": "elementName",
      "type": "table",
      "displayName": "Element name",
      "operators": [
        "in",
        "not in",
        "=",
        "<>",
        "like"
      ],
      "values": []
    },
    "operator": "like",
    "value": "*15*",
    "conjunction": "or",
    "level": 2,
    "userFriendlyValue": null
  },
  {
    "item": {
      "name": "elementName",
      "type": "table",
      "displayName": "Element name",
      "operators": [
        "in",
        "not in",
        "=",
        "<>",
        "like"
      ],
      "values": []
    },
    "operator": "like",
    "value": "*11*",
    "conjunction": "or",
    "level": 2,
    "userFriendlyValue": null
  },
  {
    "item": {
      "name": "elementName",
      "type": "table",
      "displayName": "Element name",
      "operators": [
        "in",
        "not in",
        "=",
        "<>",
        "like"
      ],
      "values": []
    },
    "operator": "in",
    "value": [
      "NEHVN_3_NOIRSC3_1",
      "NGNHU_5_NGNHU_5_1",
      "NMVL381_NMDC381_1",
      "XGR_MA1_NMDC381_1"
    ],
    "conjunction": "or",
    "level": 2,
    "userFriendlyValue": [
      "NEHVN_3_NOIRSC3_1",
      "NGNHU_5_NGNHU_5_1",
      "NMVL381_NMDC381_1",
      "XGR_MA1_NMDC381_1"
    ]
  },
  {
    "item": {
      "name": "elementType",
      "type": "select",
      "displayName": "Element type",
      "operators": [
        "=",
        "<>"
      ],
      "values": [
        "line",
        "trafo"
      ]
    },
    "operator": "=",
    "value": "trafo",
    "conjunction": "or",
    "level": 1,
    "userFriendlyValue": null
  },
  {
    "item": {
      "name": "elementName",
      "type": "table",
      "displayName": "Element name",
      "operators": [
        "in",
        "not in",
        "=",
        "<>",
        "like"
      ],
      "values": []
    },
    "operator": "in",
    "value": [
      "NBSL381_NBSL381_U",
      "NBSL381_NBSL381_V",
      "NBWK381_NBWK381_U",
      "NBWK381_NBWK381_W",
      "NBWK381_NBWK381_Y",
      "NCST381_NCST381_V",
      "NCST381_NCST381_X",
      "NCST381_NCST381_Z",
      "NDE1503_NDE1503_V",
      "NDIM381_NDIM381_V",
      "NDIM381_NDIM381_W",
      "NDIM381_NDIM381_X",
      "NDIM381_NDIM381_Y",
      "NDOD381_NDOD381_U",
      "NDOD381_NDOD381_W",
      "NDOD381_NDOD381_X",
      "NEHV381_NEHV381_W",
      "NEHV381_NEHV381_X",
      "NEHV381_NEHV381_Y",
      "NEHV381_NEHV381_Z",
      "NENS381_NENS381_T",
      "NENS381_NENS381_U",
      "NENS381_NENS381_W",
      "NHGL381_NHGL381_X",
      "NHGL381_NHGL381_Y",
      "NHGL381_NHGL381_Z",
      "NHSW222_NHSW222_X",
      "NHSW222_NHSW222_Y",
      "NHSW222_NHSW222_Z",
      "NKIJ381_NKIJ381_V",
      "NKIJ381_NKIJ381_Z",
      "NMBT381_NMBT381_U",
      "NMBT381_NMBT381_V",
      "NMBT381_NMBT381_W",
      "NMBT381_NMBT381_X",
      "NOZN381_NOZN381_W",
      "NOZN381_NOZN381_X",
      "NOZN381_NOZN381_Y",
      "NWL3801_NWL3801_X",
      "NWL3801_NWL3801_Y",
      "NWL3801_NWL3801_Z",
      "NWTR381_NWTR381_M",
      "NWTR381_NWTR381_N",
      "NWTR381_NWTR381_O"
    ],
    "conjunction": "and",
    "level": 2,
    "userFriendlyValue": [
      "NBSL381_NBSL381_U",
      "NBSL381_NBSL381_V",
      "NBWK381_NBWK381_U",
      "NBWK381_NBWK381_W",
      "NBWK381_NBWK381_Y",
      "NCST381_NCST381_V",
      "NCST381_NCST381_X",
      "NCST381_NCST381_Z",
      "NDE1503_NDE1503_V",
      "NDIM381_NDIM381_V",
      "NDIM381_NDIM381_W",
      "NDIM381_NDIM381_X",
      "NDIM381_NDIM381_Y",
      "NDOD381_NDOD381_U",
      "NDOD381_NDOD381_W",
      "NDOD381_NDOD381_X",
      "NEHV381_NEHV381_W",
      "NEHV381_NEHV381_X",
      "NEHV381_NEHV381_Y",
      "NEHV381_NEHV381_Z",
      "NENS381_NENS381_T",
      "NENS381_NENS381_U",
      "NENS381_NENS381_W",
      "NHGL381_NHGL381_X",
      "NHGL381_NHGL381_Y",
      "NHGL381_NHGL381_Z",
      "NHSW222_NHSW222_X",
      "NHSW222_NHSW222_Y",
      "NHSW222_NHSW222_Z",
      "NKIJ381_NKIJ381_V",
      "NKIJ381_NKIJ381_Z",
      "NMBT381_NMBT381_U",
      "NMBT381_NMBT381_V",
      "NMBT381_NMBT381_W",
      "NMBT381_NMBT381_X",
      "NOZN381_NOZN381_W",
      "NOZN381_NOZN381_X",
      "NOZN381_NOZN381_Y",
      "NWL3801_NWL3801_X",
      "NWL3801_NWL3801_Y",
      "NWL3801_NWL3801_Z",
      "NWTR381_NWTR381_M",
      "NWTR381_NWTR381_N",
      "NWTR381_NWTR381_O"
    ]
  }
]

我想替换一些&#34;值&#34;的内容。带有来自mysql查询的数据的键,该值将前一个值作为参数。

例如,在这个对象的情况下

  {
    "item": {
      "name": "elementName",
      "type": "table",
      "displayName": "Element name",
      "operators": [
        "in",
        "not in",
        "=",
        "<>",
        "like"
      ],
      "values": []
    },
    "operator": "in",
    "value": [
      "NEHVN_3_NOIRSC3_1",
      "NGNHU_5_NGNHU_5_1",
      "NMVL381_NMDC381_1",
      "XGR_MA1_NMDC381_1"
    ],
    "conjunction": "or",
    "level": 2,
    "userFriendlyValue": [
      "NEHVN_3_NOIRSC3_1",
      "NGNHU_5_NGNHU_5_1",
      "NMVL381_NMDC381_1",
      "XGR_MA1_NMDC381_1"
    ]
  },

如果mysql查询返回

      183213,
      834924,
      986312,
      987312

对象将成为

  {
    "item": {
      "name": "elementName",
      "type": "table",
      "displayName": "Element name",
      "operators": [
        "in",
        "not in",
        "=",
        "<>",
        "like"
      ],
      "values": []
    },
    "operator": "in",
    "value": [
      183213,
      834924,
      986312,
      987312
    ],
    "conjunction": "or",
    "level": 2,
    "userFriendlyValue": [
      "NEHVN_3_NOIRSC3_1",
      "NGNHU_5_NGNHU_5_1",
      "NMVL381_NMDC381_1",
      "XGR_MA1_NMDC381_1"
    ]
  },

我打算用jq&#34;#0;&#34;。[0]&#34;循环遍历第一个数组。 &#34; [1]&#34;并在循环中连接生成的json。

是否有更聪明或更好的方式?我可以看到jq有循环,但我没有成功使用它们。

2 个答案:

答案 0 :(得分:0)

jq 解决方案:

假设新值以包含逗号分隔项的字符串形式出现。变量new_values将包含这些新值。

export new_values="736231,937213,297312,923723,913346,642354,844737,398301,943401,472312,483423,483324,432123,394610,473412,472311,956121,786052,879411,461223,123999,598111,865511,987917,576914,691044,765991,987152,979166,987111,283113,987999,112311,768123,687688,987123,212000,198623,897777,123111,387661,662111,190231,761900"

jq --arg new_vals "$new_values" '.[] | if(.item.name == "elementName" and .operator == "in")
                   then .value |= ($new_vals | split(",")) else empty end' input.json

输出:

{
  "item": {
    "name": "elementName",
    "type": "table",
    "displayName": "Element name",
    "operators": [
      "in",
      "not in",
      "=",
      "<>",
      "like"
    ],
    "values": []
  },
  "operator": "in",
  "value": [
    "736231",
    "937213",
    "297312",
    "923723",
    "913346",
    "642354",
    "844737",
    "398301",
    "943401",
    "472312",
    "483423",
    "483324",
    "432123",
    "394610",
    "473412",
    "472311",
    "956121",
    "786052",
    "879411",
    "461223",
    "123999",
    "598111",
    "865511",
    "987917",
    "576914",
    "691044",
    "765991",
    "987152",
    "979166",
    "987111",
    "283113",
    "987999",
    "112311",
    "768123",
    "687688",
    "987123",
    "212000",
    "198623",
    "897777",
    "123111",
    "387661",
    "662111",
    "190231",
    "761900"
  ],
  "conjunction": "or",
  "level": 2,
  "userFriendlyValue": [
    "NEHVN_3_NOIRSC3_1",
    "NGNHU_5_NGNHU_5_1",
    "NMVL381_NMDC381_1",
    "XGR_MA1_NMDC381_1"
  ]
}
{
  "item": {
    "name": "elementName",
    "type": "table",
    "displayName": "Element name",
    "operators": [
      "in",
      "not in",
      "=",
      "<>",
      "like"
    ],
    "values": []
  },
  "operator": "in",
  "value": [
    "736231",
    "937213",
    "297312",
    "923723",
    "913346",
    "642354",
    "844737",
    "398301",
    "943401",
    "472312",
    "483423",
    "483324",
    "432123",
    "394610",
    "473412",
    "472311",
    "956121",
    "786052",
    "879411",
    "461223",
    "123999",
    "598111",
    "865511",
    "987917",
    "576914",
    "691044",
    "765991",
    "987152",
    "979166",
    "987111",
    "283113",
    "987999",
    "112311",
    "768123",
    "687688",
    "987123",
    "212000",
    "198623",
    "897777",
    "123111",
    "387661",
    "662111",
    "190231",
    "761900"
  ],
  "conjunction": "and",
  "level": 2,
  "userFriendlyValue": [
    "NBSL381_NBSL381_U",
    "NBSL381_NBSL381_V",
    "NBWK381_NBWK381_U",
    "NBWK381_NBWK381_W",
    "NBWK381_NBWK381_Y",
    "NCST381_NCST381_V",
    "NCST381_NCST381_X",
    "NCST381_NCST381_Z",
    "NDE1503_NDE1503_V",
    "NDIM381_NDIM381_V",
    "NDIM381_NDIM381_W",
    "NDIM381_NDIM381_X",
    "NDIM381_NDIM381_Y",
    "NDOD381_NDOD381_U",
    "NDOD381_NDOD381_W",
    "NDOD381_NDOD381_X",
    "NEHV381_NEHV381_W",
    "NEHV381_NEHV381_X",
    "NEHV381_NEHV381_Y",
    "NEHV381_NEHV381_Z",
    "NENS381_NENS381_T",
    "NENS381_NENS381_U",
    "NENS381_NENS381_W",
    "NHGL381_NHGL381_X",
    "NHGL381_NHGL381_Y",
    "NHGL381_NHGL381_Z",
    "NHSW222_NHSW222_X",
    "NHSW222_NHSW222_Y",
    "NHSW222_NHSW222_Z",
    "NKIJ381_NKIJ381_V",
    "NKIJ381_NKIJ381_Z",
    "NMBT381_NMBT381_U",
    "NMBT381_NMBT381_V",
    "NMBT381_NMBT381_W",
    "NMBT381_NMBT381_X",
    "NOZN381_NOZN381_W",
    "NOZN381_NOZN381_X",
    "NOZN381_NOZN381_Y",
    "NWL3801_NWL3801_X",
    "NWL3801_NWL3801_Y",
    "NWL3801_NWL3801_Z",
    "NWTR381_NWTR381_M",
    "NWTR381_NWTR381_N",
    "NWTR381_NWTR381_O"
  ]
}

答案 1 :(得分:0)

以下是使用jq--argfile选项的一般解决方案。首先创建一个替换表,但是你喜欢。 e.g。

[
  {
    "from": ["NEHVN_3_NOIRSC3_1","NGNHU_5_NGNHU_5_1","NMVL381_NMDC381_1","XGR_MA1_NMDC381_1"],
    "to":   [183213, 834924, 986312, 987312]
  }
]

如果此表位于repl.json文件中,则您的数据位于data.json,以下过滤器位于filter.jq

文件中
  $replacements[] as $r
| map(if .value==$r.from then .value=$r.to else . end)

然后命令

$ jq -M --argfile replacements repl.json -f filter.jq data.json

将生成带有替换值的输出。