2017-09-06 2 views
-2

ich die folgende JSON-Daten haben:ersetzen Werte in 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" 
    ] 
    } 
] 

Ich mag würde, den Inhalt von einigen der „Wert“ Schlüssel mit Daten aus einer MySQL-Abfrage ersetzen, die den vorherigen Wert als Argument verwendet.

Zum Beispiel in dem Fall dieses Objekt

{ 
    "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" 
    ] 
    }, 

wenn die mysql query

 183213, 
     834924, 
     986312, 
     987312 

das Objekt

{ 
    "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" 
    ] 
    }, 

würde zurückgekehrt Ich hatte geplant, über die Schleife erstes Array mit jqs ". [0]" ". [1]" und verkettete resultierende json in der Schleife.

Gibt es einen schlaueren oder allgemein besseren Weg? Ich kann sehen, dass jq Schleifen hat, aber ich konnte sie nicht verwenden.

+1

Post das gewünschte Ergebnis – RomanPerekhrest

+0

Es ist da. Danke, dass du darauf hingewiesen hast. – Gryffus

Antwort

0

jq Lösung:

Unter der Annahme, dass die neuen Werte werden als Zeichenfolge durch Komma getrennte Teile enthält. Die Variable new_values enthält diese neuen Werte.

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 

Der Ausgang:

{ 
    "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" 
    ] 
} 
0

Hier ist eine allgemeine Lösung jq und die --argfile Option. Erstellen Sie zunächst eine Ersatztabelle, wie Sie möchten. z.B.

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

Wenn diese Tabelle in der repl.json Datei ist, sind Ihre Daten in data.json und die folgenden Filter in der Datei filter.jq

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

dann der Befehl

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

wird die produzieren Ausgabe mit den ersetzten Werten.