1

I have a json which consists a json array 'attrs'. This array contains name/value pairs so the number of objects inside it is dynamic. I know 2 name/value pairs that will always be present and i'm currently using jolt to extract them and bring them to the outermost level.
During this process, I'm seeing three scenarios

Jolt Spec used :

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

Scenario 1: All name/value pairs in 'attrs' were the ones I was expecting, therefor after using jolt nothing was left inside this 'attrs' and in the transformed json, this 'attrs' was not present. This was expected.

Input JSON :

[
  {
    "requestedStartDate": null,
    "cuDetails": [
      {
        "role": "Customer",
        "value": "ABX"
      },
      {
        "role": "ABC",
        "value": "ABC"
      }
    ],
    "notes": null,
    "attrs": [
      {
        "name": "orderType",
        "value": "PROVPN"
      },
      {
        "name": "paymentType",
        "value": "Upgrade/Downgrade/ChangeContractPeriod"
      }
    ]
  }
]

Output JSON :

[
  {
    "requestedStartDate": null,
    "cuDetails": [
      {
        "role": "Customer",
        "value": "ABX"
      },
      {
        "role": "ABC",
        "value": "ABC"
      }
    ],
    "notes": null,
    "orderType": "PROVPN",
    "paymentType": "Upgrade/Downgrade/ChangeContractPeriod"
  }
]

Scenario 2 : Along with the name/value pairs I was expecting, there was ONE extra name/value pair. After transformation, the expected name/value pairs are in the outermost level but the 'attrs' is transformed as an OBJECT. This was not expected, I was expecting 'attrs' to remain an array.

Input JSON :

[
  {
    "requestedStartDate": null,
    "cuDetails": [
      {
        "role": "Customer",
        "value": "ABX"
      },
      {
        "role": "ABC",
        "value": "ABC"
      }
    ],
    "notes": null,
    "attrs": [
      {
        "name": "orderType",
        "value": "PROVPN"
      },
      {
        "name": "paymentType",
        "value": "Upgrade/Downgrade/ChangeContractPeriod"
      },
      {
        "name": "deliveryType",
        "value": "CRM"
      }
    ]
  }
]

Output JSON :

[
  {
    "requestedStartDate": null,
    "cuDetails": [
      {
        "role": "Customer",
        "value": "ABX"
      },
      {
        "role": "ABC",
        "value": "ABC"
      }
    ],
    "notes": null,
    "orderType": "PROVPN",
    "paymentType": "Upgrade/Downgrade/ChangeContractPeriod",
    "attrs": {
      "name": "deliveryType",
      "value": "CRM"
    }
  }
]

Scenario 3 : Along with the name/value pairs I was expecting, there were multiple extra name/value pair. After transformation, the expected name/value pairs are in the outermost level but the 'attrs' is transformed as an ARRAY. This was expected.

Input JSON :

[
  {
    "requestedStartDate": null,
    "cuDetails": [
      {
        "role": "Customer",
        "value": "ABX"
      },
      {
        "role": "ABC",
        "value": "ABC"
      }
    ],
    "notes": null,
    "attrs": [
      {
        "name": "orderType",
        "value": "PROVPN"
      },
      {
        "name": "paymentType",
        "value": "Upgrade/Downgrade/ChangeContractPeriod"
      },
      {
        "name": "deliveryType",
        "value": "CRM"
      },
      {
        "name": "deliveryAddress",
        "value": "aBC"
      }
    ]
  }
]

Output JSON :

[
  {
    "requestedStartDate": null,
    "cuDetails": [
      {
        "role": "Customer",
        "value": "ABX"
      },
      {
        "role": "ABC",
        "value": "ABC"
      }
    ],
    "notes": null,
    "orderType": "PROVPN",
    "paymentType": "Upgrade/Downgrade/ChangeContractPeriod",
    "attrs": [
      {
        "name": "deliveryType",
        "value": "CRM"
      },
      {
        "name": "deliveryAddress",
        "value": "aBC"
      }
    ]
  }
]

Expected Output in Scenario 2 & 3 : In case that attrs JSON array is displayed, i want it to be displayed as an array not an object .

1 Answer 1

0

In any case the following spec will return an array for extra attrs by adding square brackets [] :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&",
        "attrs": {
          "*": {
            "name": {
              "*": { // else case for "name" matches
                "@2": "[&5].&4[]"
              },
              "orderType|paymentType": {
                "@2,value": "[&5].&1"
              }
            }
          }
        }
      }
    }
  }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.