Removing and printing name/value pair from json using jolt

Viewed 27

I want to remove a name/value pair from inside a json array and print it outside. I started by trying this and then expanding the whole request to be a json array. The solution mentioned above does not seem to be working.

Input :

[
  {
    "createdBy": "Admin",
    "createdDate": "2022-09-08",
    "modifiedBy": "Admin",
    "attrs": [
      {
        "name": "Type",
        "value": "Postpaid"
      },
      {
        "name": "subscriber",
        "value": "Paid"
      },
      {
        "name": "Details",
        "value": {
          "createdDate": "today",
          "description": "offer",
          "id": null
        }
      }
    ],
    "relatedInfo": [
      {
        "type": "Number",
        "name": "000000"
      },
      {
        "type": "Type",
        "name": "Post"
      }
    ]
  },
  {
    "createdBy": "Admin",
    "createdDate": "2022-09-08",
    "modifiedBy": "Admin",
    "attrs": [
      {
        "name": "Type",
        "value": "Postpaid"
      },
      {
        "name": "subscriber",
        "value": "Paid"
      },
      {
        "name": "Details",
        "value": {
          "createdDate": "today",
          "description": "offer",
          "id": null
        }
      }
    ],
    "relatedInfo": [
      {
        "type": "Number",
        "name": "000000"
      },
      {
        "type": "Type",
        "name": "Post"
      }
    ]
  }
]

Desired Output :

[
  {
    "createdBy": "Admin",
    "createdDate": "2022-09-08",
    "modifiedBy": "Admin",
    "attrs": [
      {
        "name": "Type",
        "value": "Postpaid"
      },
      {
        "name": "subscriber",
        "value": "Paid"
      }
    ],
    "Details": {
      "createdDate": "today",
      "description": "offer",
      "id": null
    },
    "relatedInfo": [
      {
        "type": "Number",
        "name": "000000"
      },
      {
        "type": "Type",
        "name": "Post"
      }
    ]
  },
  {
    "createdBy": "Admin",
    "createdDate": "2022-09-08",
    "modifiedBy": "Admin",
    "attrs": [
      {
        "name": "Type",
        "value": "Postpaid"
      },
      {
        "name": "subscriber",
        "value": "Paid"
      }
    ],
    "Details": {
      "createdDate": "today",
      "description": "offer",
      "id": null
    },
    "relatedInfo": [
      {
        "type": "Number",
        "name": "000000"
      },
      {
        "type": "Type",
        "name": "Post"
      }
    ]
  }
]    

Current Jolt spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": "[&]",
      "attrs": {
        "*": {
          "name": {
            "*": { "@2": "&4" },
            "Details": {
              "@(2,value)": "&1"
            }
          }
        }
      }
    }
  }
]

I can't seem to figure out how the jolt spec would change in case of the array

1 Answers

So far so good, just need to combine the attributes at a common node. To do this, I've used the identifiers [&1] and [&5] in order to reach the level of the outermost index within the tree such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&",
        "attrs": {
          "*": {
            "name": {
              "*": { 
                "@2": "[&5].&4" 
              },
              "Details": {
                "@(2,value)": "[&5].&1"
              }
            }
          }
        }
      }
    }
  }
]
Related