How to extract value from JSON array object and assign to Assign message

I am having an issue extracting values from a response, and assigning them to the output object. 

My current response is as below 

 

{
  "data": {
    "translations": [
      {
        "translatedText": "Hallo Welt!",
        "detectedSourceLanguage": "en"
      }
    ]
  }
}

I am trying to modify the output response with the below code of assign message policy

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-BuildTranslateResponse">
    <DisplayName>AM-BuildTranslateResponse</DisplayName>
    <Variable name="SList">
        <JSONPath>$.affiliate[*]</JSONPath>
    </Variable>
    <AssignVariable>
        <Name>affiliate </Name>
        <Template>{jsonPath($.results[0].formatted_address, response.content) } </Template>
    </AssignVariable>
    <Set>
        <Payload contentType="application/json">
      {"target":"{affiliate}", "q":"{SList}"}
    </Payload>
        <Verb>POST</Verb>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="true" transport="http" type="response"/>
</AssignMessage>

 

Can anyone guide me how can i resolve this 

 

Solved Solved
0 1 3,725
1 ACCEPTED SOLUTION

I do not understand what you are trying to do. It seems like you are trying to extract things out of a JSON payload, via jsonpath. That much is clear. But there are a few problems.

1. the jsonpath expression in your AssignVariable/Template element ... does not correspond to anything that is in the example JSON payload you showed. I think you need to debug your jsonpath expressions. You can try an online jsonpath comparison tool to do that. like maybe this one. For the given payload, this is a reasonable jsonpath expression: $.data.translations[0].translatedText . For your example payload, this is not a reasonable expression: $.results[0].formatted_address It does not refer to anything in the payload.

2. It is not valid to use AssignMessage/Variable. Variable is not a valid child element of AssignMessage. I don't know what you're doing there.

3. If you use jsonpath() function within the Template element, you must remove all spaces.


Assuming that there is a MESSAGE variable named contrived-message, with the content of

 

{
  "data": {
    "translations": [
      {
        "translatedText": "Hallo Welt!",
        "detectedSourceLanguage": "en"
      }
    ]
  }
}

 

...then this policy:

 

<AssignMessage name='AM-Response'>
    <AssignVariable>
      <Name>SList</Name>
      <!-- not sure what is going on here -->
      <Value>$.affiliate[*]</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>path1</Name>
        <Value>$.data.translations[0].translatedText</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>extracted-text</Name>
        <Template>{jsonPath(path1,contrived-message.content)}</Template>
    </AssignVariable>
    <Set>
        <Payload contentType="application/json">{
  "target":"{extracted-text}",
  "q":"{SList}"
}
</Payload>
        <!-- verb applies only to Request messages -->
        <!--
            <Verb>POST</Verb>
        -->
    <ReasonPhrase>OK</ReasonPhrase>
    <StatusCode>200</StatusCode>
    </Set>
    <AssignTo>response</AssignTo>
</AssignMessage>

 

...produces this result:

 


{
  "target":"Hallo Welt!",
  "q":"$.affiliate[*]"
}

 

View solution in original post

1 REPLY 1

I do not understand what you are trying to do. It seems like you are trying to extract things out of a JSON payload, via jsonpath. That much is clear. But there are a few problems.

1. the jsonpath expression in your AssignVariable/Template element ... does not correspond to anything that is in the example JSON payload you showed. I think you need to debug your jsonpath expressions. You can try an online jsonpath comparison tool to do that. like maybe this one. For the given payload, this is a reasonable jsonpath expression: $.data.translations[0].translatedText . For your example payload, this is not a reasonable expression: $.results[0].formatted_address It does not refer to anything in the payload.

2. It is not valid to use AssignMessage/Variable. Variable is not a valid child element of AssignMessage. I don't know what you're doing there.

3. If you use jsonpath() function within the Template element, you must remove all spaces.


Assuming that there is a MESSAGE variable named contrived-message, with the content of

 

{
  "data": {
    "translations": [
      {
        "translatedText": "Hallo Welt!",
        "detectedSourceLanguage": "en"
      }
    ]
  }
}

 

...then this policy:

 

<AssignMessage name='AM-Response'>
    <AssignVariable>
      <Name>SList</Name>
      <!-- not sure what is going on here -->
      <Value>$.affiliate[*]</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>path1</Name>
        <Value>$.data.translations[0].translatedText</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>extracted-text</Name>
        <Template>{jsonPath(path1,contrived-message.content)}</Template>
    </AssignVariable>
    <Set>
        <Payload contentType="application/json">{
  "target":"{extracted-text}",
  "q":"{SList}"
}
</Payload>
        <!-- verb applies only to Request messages -->
        <!--
            <Verb>POST</Verb>
        -->
    <ReasonPhrase>OK</ReasonPhrase>
    <StatusCode>200</StatusCode>
    </Set>
    <AssignTo>response</AssignTo>
</AssignMessage>

 

...produces this result:

 


{
  "target":"Hallo Welt!",
  "q":"$.affiliate[*]"
}