Neeraj
Neeraj

Reputation: 49

convert json data in key value by jolt processor

I have following Input and i want to convert the data in expected output by jolt processor

Input

{
  "tenantId": "master",
  "transactionId": "838",
  "alertArray": [
    {
      "alertType": "info",
      "priority": "low",
      "data": "new",
      "properties": [
        {
          "ConnectorID": "1",
          "ConnectorType": "IEC_60309",
          "IDTag": "A5270529",
          "abc": "xyz",
          "pqr": "rst",
          "bals": "ahjdgcd"
        }
      ]
    }
  ]
}

Expected output

{
  "tenantId": "master",
  "transactionId": "838",
  "alertArray": [
    {
      "alertType": "info",
      "priority": "low",
      "data": "new",
      "properties": [
        {
          "key": "ConnectorID",
          "value": "2"
        },
        {
          "key": "ConnectorType",
          "value": "IEC_60309"
        },
        {
          "key": "DTag",
          "value": "A5270529"
        },
        {
          "key": "abc",
          "value": "xyz"
        },
        {
          "key": "pqr",
          "value": "rst"
        },
        {
          "key": "bals",
          "value": "ahjdgcd"
        }
      ]
    }
  ]
}

we have data that contain properties array properties array contain pair of data and i want to convert the pair of data into key and value

Upvotes: 0

Views: 69

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65433

You can use this shift transformation after diving deep into the attributes under properties array and use @ and $ symbols to match val and key pairs respectively

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "alertA*": {
        "*": {
          "*": "&2[&1].&", //&2 is to substitute "alertArray" after going tree 2 levels up along with its indexes([&1]), and the rightmost & is replicates the current level's values 
          "pro*": {
            "*": {
              "*": {
                "$": "&5[&4].&3[#2].key", // &5 : "alertArray", [&4] : indexes of "alertArray", &3 : "properties", indexes of "properties"
                "@": "&5[&4].&3[#2].value"
              }
            }
          }
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is :

enter image description here

Upvotes: 1

Related Questions