JSON extraction

Not applicable

Hi , I need to extract the "value" from below json message and assign to a variable based on the index value. Can someone help me how to achieve this.

Input Json :

{ "issuance_params": { "reconciliation_fields": { "user_defined_fields_group": [ { "index": 1, "value": "test1" }, { "index": 2, "value": "test2" } ] } } }

Output:

userDefinedField1=test1

userDefinedField2=test2

Thanks.

Solved Solved
0 6 4,139
1 ACCEPTED SOLUTION

HI @Nagabhairu Babu

Welcome to the community !

You can do it using Extract Variables policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="extract-response">
    <DisplayName>extract-response</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="values" type="nodeset">
            <JSONPath>$.issuance_params.reconciliation_fields.user_defined_fields_group..value</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">response</Source>
    <VariablePrefix>responsePrefix</VariablePrefix>
</ExtractVariables>

You will get an array stored in the flow variable "responsePrefix.values". You can use a Javascript policy to get the values and continue using these values according to your business logic.

Also, to test the JSON path in the policy, you can test it using jsonpath.com

Let me know if it worked and if it did, please accept the answer.

View solution in original post

6 REPLIES 6

HI @Nagabhairu Babu

Welcome to the community !

You can do it using Extract Variables policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="extract-response">
    <DisplayName>extract-response</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="values" type="nodeset">
            <JSONPath>$.issuance_params.reconciliation_fields.user_defined_fields_group..value</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">response</Source>
    <VariablePrefix>responsePrefix</VariablePrefix>
</ExtractVariables>

You will get an array stored in the flow variable "responsePrefix.values". You can use a Javascript policy to get the values and continue using these values according to your business logic.

Also, to test the JSON path in the policy, you can test it using jsonpath.com

Let me know if it worked and if it did, please accept the answer.

Hi Sai Saran Vaidyanathan , Thank you for the prompt response.

Sorry I miss to update earlier, My input json does not maintain the order and can skip few indexes.

example below , index3 is not present.

{ "issuance_params": { "reconciliation_fields": { "user_defined_fields_group": [ { "index": 1, "value": "test1" }, { "index": 2, "value": "test2" }, { "index": 4, "value": "test4" } ] } } }

Output should be ..

userDefinedField1=test1

userDefinedField2=test2

userDefinedField3=""

userDefinedField4=test4

Seems the above solution does n't work. I am thinking to use java script to read the values based on the indexes present and assign to the corresponding fields. Please advise ?

Thanks.

HI

In that case you will need to extract both value and index

<JSONPayload>
        <Variable name="index" type="nodeset">
            <JSONPath>$.issuance_params.reconciliation_fields.user_defined_fields_group..index</JSONPath>
        </Variable>
        <Variable name="value" type="nodeset">
            <JSONPath>$.issuance_params.reconciliation_fields.user_defined_fields_group..value</JSONPath>
        </Variable>
    </JSONPayload>

I came up with a quick Javascript policy to do the mapping as you were looking for one. I am assuming the index is sorted in the ascending order of the index. We need it sorted so that we can use the last element of the index array to set the array size of the new array we are building. If the response index is not in ascending order, create another Variable in the Extract Variables and within the Javascript policy use the arrays.sort method and get the last value and use that to create the new array.

Javascript code:

var index = JSON.parse(context.getVariable("responsePrefix.index"));
var value = JSON.parse(context.getVariable("responsePrefix.value"));

//New Array
var newResponseArray = [];
newResponseArray.length = index[(index.length)-1]; //Fetching the last element from the index array

//Iterate through the index array, get the index and set that as the position on the new array and the appropriate value.
for(var i=0; i<index.length; i++){
    newResponseArray[index[i]-1] = value[i];
}


print(newResponseArray);

On printing the new array, the array should be

test1,test2,,test4

Now you have an array with both positions (used from index) and the appropriate value. Instead of using different variables, you can use the same array with index positions to control the logic

This was just a quick thought of how it could be done. Please test and I am sure there could be many other solutions (more optimized than this). But just wanted to give you a head start.

Hope this helps 🙂

Thank you Sai , Appreciate your help !!

Thank you @Sai Saran Vaidyanathan ,

@naga , Welcome to Apigee Community & Gald your issue is resolved. If above answer solved your issue, please click on accept link below the answer to mark it as resolved so that it will be helpful for others too. Thank you 🙂

Thanks @Anil Sagar