1

I am trying to convert the below JSON payload into a JSON which has the field name as the value of the field: The jolt file that I have is working if field values are different. But if field values are the same then it is giving an array in the response.

Can you please help to provide jolt specifications for this?

Input JSON Payload :

{
  "action": {
    "allowPartialSuccess": true,
    "records": [
      {
        "ZuoraSubscriptionExtId": "962041ad-e673-492a-a071-5e0ab74ea001",
        "CPQSubscriptionID__c": "5640029343"
      },
      {
        "ZuoraSubscriptionExtId": "962041ad-e673-492a-a071-5e0ab74ea001",
        "CPQSubscriptionID__c": "5640029343"
      }
    ],
    "type": "update"
  }
}

`

Expected output JSON Payload :

{
  "action" : {
    "allowPartialSuccess" : true,
    "records" :  {
      "962041ad-e673-492a-a071-5e0ab74ea001" : {
        "CPQSubscriptionID__c" : "5640029343"
      }
    }, {
      "962041ad-e673-492a-a071-5e0ab74ea001" : {
        "CPQSubscriptionID__c" : "5640029343"
      }
    } ,
    "type" : "update"
  }
}

JOLT specification that I am using :

[
  {
    "operation": "shift",
    "spec": {
      "action": {
        "allowPartialSuccess": "action.allowPartialSuccess",
        "records": {
          "*": {
            "CPQSubscriptionID__c": "action.records.@(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
          }
        },
        "type": "action.type"
      }
    }
  }
]
1
  • 1
    Your expected output is wrong. you cant have 2 same object in your record that is not array Commented Dec 20, 2022 at 11:23

2 Answers 2

2

You can use this shift transformation spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1.&", // &1 replicates the literal "action"
        "records": {
          "*": {
            "C*": "&3.&2.@(1,ZuoraSubscriptionExtId).&" // &3 replicates the literal "action" (by going three level up the tree), &2 for "records", & replicates the current level attribute
          }
        }
      }
    }
  }
]

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

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

But, This answer has the same result!
No, not the same, but I think that you wanted to notify for the extra object wrapper comes after "records", but that would make it an array rather than an object, eg. the desired output should presumably be two consecutive objects with different labels. Otherwise that is already a wrong JSON value. @MohammadReza
If assume desired output is an object we have the same ids for that. so to prevent the same values that create an array in output, we need a cardinality spec.
1

Your expected output is wrong in the question.

If you want an object in your record, You can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "&": "&1.&",
        "records": {
          "*": {
            "CPQSubscriptionID__c": "&3.&2.@(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "records": {
          "*": {
            "*": "ONE"
          }
        }
      }
    }
  }
]

And if you want an array in your output. You can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1.&",
        "records": {
          "*": {
            "CPQSubscriptionID__c": "&3.&2[].@(1,ZuoraSubscriptionExtId).CPQSubscriptionID__c"
          }
        }
      }
    }
  }
]

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.