How to referente an array element

I`ve extracted an array from a Json Input and i want to make referente to one elemente on array, when i try to do this i can see that Apigee is trying to make referente to a new variable.

as you can see below, i have the "jsonPath.result" as an array, but in assignMessage i try to read "jsonPath.result[2]", its a blank value and should be "VW".

How can i referente do specific elements inside an array?

Solved Solved
0 2 814
1 ACCEPTED SOLUTION

Thanks for the screenshot, that makes it very clear.

Apigee context variables are strongly typed: they can be of type Message, boolean, String, and so on.

I believe the extraction you've done has populated a variable with a string, which looks like this:

["Ford", "BMW", "VW"]

I don't know for sure, though. Your JavaScript is the thing that is storing this variable, probably with a statement like this:

context.setVariable('jsonPath.result', SOMETHING);

...and I'm guessing the SOMETHING resolves to a string.

Anyway, within the Apigee DSL, it is not possible to coerce a string to behave as an Array, or to implicitly cast a string into an array. Using a name like "jsonPath.result[2]" will not do what you hope.

The way to get access to the 2nd item in the array is

  • within Javascript itself, referencing the variable scoped to the SCRIPT, not the context variable in the Apigee message context
  • If you apply a second jsonPath extraction , via ExtractVariables or otherwise, on the jsonPath.result variable.

View solution in original post

2 REPLIES 2

Thanks for the screenshot, that makes it very clear.

Apigee context variables are strongly typed: they can be of type Message, boolean, String, and so on.

I believe the extraction you've done has populated a variable with a string, which looks like this:

["Ford", "BMW", "VW"]

I don't know for sure, though. Your JavaScript is the thing that is storing this variable, probably with a statement like this:

context.setVariable('jsonPath.result', SOMETHING);

...and I'm guessing the SOMETHING resolves to a string.

Anyway, within the Apigee DSL, it is not possible to coerce a string to behave as an Array, or to implicitly cast a string into an array. Using a name like "jsonPath.result[2]" will not do what you hope.

The way to get access to the 2nd item in the array is

  • within Javascript itself, referencing the variable scoped to the SCRIPT, not the context variable in the Apigee message context
  • If you apply a second jsonPath extraction , via ExtractVariables or otherwise, on the jsonPath.result variable.