'Get a specific string from JSON string where "string" = x | Freemarker

My problem is as follows. I get a JSON string from an api call which looks like that:

enter image description here

I always want to know the timestamp at which the package has been picked up by a courier.

The essential problem here is that the pickup is not always the same number in the "events" branch of the string. But the "code" is always 3.0 or something. It would be not a problem if the "events" are always at the same place. Then I could do something, its just an example, like this:

<#assign someParsed = responseContent?eval_json>
${someParsed.parcels[0].events[0].timestamp}

So how do I get the bottom line of the code if the position is not always at the same position?



Solution 1:[1]

Assuming there's always a code 3.0 (otherwise of course you need to test for that), you can do it like this:

${someParsed.parcels[0].events?filter(it -> it.code == '3.0')[0].timestamp}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ddekany