Home > front end >  Jolt for input array to name value pair of array
Jolt for input array to name value pair of array

Time:12-29

Input is :

{
  "Size": "2",
  "done": "true",
  "records": [
    {
      
      "Id": "a7g6s0000004GZuAAM",
      "NN": "00096411.0",
      "Name": "ISOLIN TRADE & INVEST"
    },
    {
      
      "Id": "a7g6s0000004GZzAAM",
      "Number": "00096412.0",
      "Name": "ISOLIN"
    }
  ]
}

Spec used:

[
  {
    "operation": "remove",
    "spec": {
      "records": {
        "*": {
          "attributes": " "
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "records": {
        "*": { //iterate on each object of records
          "*": { //iterate on each element of object
            "$": "Items[#1].Fields[].Name",
            "@": "Items[#1].Fields[].Value"
          }
        }
      }
    }
  }
]

Current Output:

{
  "Size": "2",
  "done": "true",
  "Items": [
    {
      "Fields": [
        {
          "Name": "Id"
        },
        {
          "Value": "a7g6s0000004GZuAAM"
        },
        {
          "Name": "NN"
        },
        {
          "Value": "00096411.0"
        },
        {
          "Name": "Name"
        },
        {
          "Value": "ISOLIN TRADE & INVEST"
        },
        {
          "Name": "Id"
        },
        {
          "Value": "a7g6s0000004GZzAAM"
        },
        {
          "Name": "Number"
        },
        {
          "Value": "00096412.0"
        },
        {
          "Name": "Name"
        },
        {
          "Value": "ISOLIN"
        }
      ]
    }
  ]
}

Expected output:

{
    "Size": "2",
    "done": "true",
    "Items": [
        {
            "Fields": [
                {
                    "Name": "Id",
                    "Value": "a7g6s0000004GZuAAM"
                },
                {
                    "Name": "NN",
                    "Value": "00096411.0"
                },
                {
                    "Name": "Name",
                    "Value": "ISOLIN TRADE & INVEST"
                },
                {
                    "Name": "Id",
                    "Value": "a7g6s0000004GZzAAM"
                },
                {
                    "Name": "Number",
                    "Value": "00096412.0"
                },
                {
                    "Name": "Name",
                    "Value": "ISOLIN"
                }
            ]
        }
    ]
}

CodePudding user response:

You can seperate those attributes into individual objects through use of Items.&2.[#2]. pattern as prefix such as

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "records": {
        "*": {
          "*": {
            "$": "Items.&2.[#2].Name",
            "@": "Items.&2.[#2].Value"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "Items": {
        "*": {
          "*": "Fields[]"
        }
      }
    }
  }
]

enter image description here

  • Related