simple jsonPath expression doesn't work

Hello there,

I am trying to parse the request.content and assign it to a variable which I will use later to set the request body for downstream. I am using AssignMessage -> Template -> jsonPath as you can see below. But, I get the error below, it seems to ignore the comma and consider both the parameters passed to jsonPath as one variable. 

{
  "fault": {
    "faultstring": "Unresolved variable : $.results.address.line1, response.content",
    "detail": {
      "errorcode": "entities.UnresolvedVariable"
    }
  }
}
<AssignMessage name="AM-BuildTranslateRequest">
  <AssignVariable>
    <Name>language</Name>
    <Template>{firstnonnull(request.queryparam.lang, propertyset.language.output)} </Template>
  </AssignVariable>
  <AssignVariable>
    <Name>text</Name>
    <Template>{jsonPath($.results.address.line1, response.content)} </Template>
  </AssignVariable>
  <Set>
    <Payload contentType="application/json">{"q":"{text}","target":"{language}"}</Payload>
    <Verb>POST</Verb>
  </Set>
  <AssignTo createNew="false"/>
</AssignMessage>

 

I tried with a simple JSON request object as well, i.e., { "text" : "Hello World!" } and jsonPath($.text, request.content). Even in this case, it ignores the comma in the middle and tries to resolve the variable $.text,request.content.

I tried putting double quotes around and space after comma etc., but nothing worked.

I also tried couple of other jsonPath expressions as mentioned in apigee docs, but these ones return empty values. For example, the below jsonPath expression for a relevant input returns empty

<AssignVariable>
<Name>translated</Name>
<Template>{jsonPath($.data.translations[0].translatedText, message.content)}</Template>
</AssignVariable>

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

Please sugges, as this is where I get struck and I failed the Challenge Lab.

Thanks

Vaseem

 

0 3 454
3 REPLIES 3

For me it failed at javascript policy. It was not able to resolve the javascript policy variable. And I have no experience in JavaScript, so I am stuck in the lab 2 itself.

try this

<AssignMessage name="AM-BuildTranslateRequest">
  <AssignVariable>
    <Name>language</Name>
    <Template>{firstnonnull(request.queryparam.lang, propertyset.language.output)} </Template>
  </AssignVariable>
  <AssignVariable>
    <Name>path1</Name>
    <Value>$.results.address.line1</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>text</Name>
    <Template>{jsonPath(path1,response.content)}</Template>
  </AssignVariable>
  <Set>
    <Payload contentType="application/json">{"q":"{text}","target":"{language}"}</Payload>
    <Verb>POST</Verb>
  </Set>
  <AssignTo createNew="false"/>
</AssignMessage>

Example with JS:

request content:

{
  "city":"hyd",
  "name":"abc"
}

Javascript

var res= JSON.parse(context.proxyRequest.content);

//read value from the request
var cityName=res.city;
print(city);
var EmpName=res.name;
print(EmpName);