Rearraning of fields in nested JSON using Jolt

Viewed 19

I have a requirement where re-arrangement of fields need to be done in a nested JSON which has array objects.

Below is the sample input:

{
  "_id": "1234",
  "Name": "John",
  "City": "New Jersey",
  "Order": [
    {
      "item": "pizza",
      "qty": 2
    },
    {
      "item": "coke",
      "qty": 2
    }
  ],
  "State": "NJ",
  "Phone": "67755321",
  "Paymenttype": "Card"
}

My Jolt is able to populate all fields, but unable to figure out how to get the 'order data' in same manner in output

My code:

[
  {
    "operation": "shift",
    "spec": {
      "_id": "_id",
      "Name": "Name",
      "State": "State",
      "Phone": "Phone"
    }
  }
]

Expected output:

{
  "_id" : "1234",
  "Name" : "John",
  "State" : "NJ",
  "Phone" : "67755321",
  "Order": [
    {
      "item": "pizza",
      "qty": 2
    },
    {
      "item": "coke",
      "qty": 2
    }
  ],
  "Paymenttype": "Card"
}

1 Answers

You can just use ampersand substitutions for the values of the attributes such as

[
  {
    "operation": "shift",
    "spec": {
      "_id": "&",
      "Name": "&",
      "State": "&",
      "Phone": "&",
      "Order": "&",
      "Paymenttype": "&"
    }
  }
]

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

enter image description here

even using "*" wildcards within the keys might be practical(depending on the existing key names) such as

[
  {
    "operation": "shift",
    "spec": {
      "_id": "&",
      "N*": "&",
      "St*": "&",
      "Ph*": "&",
      "Or*": "&",
      "Pa*": "&"
    }
  }
]
Related