Parsing json where it has array inside

I got a JSON text which I should parse, but for some reason I can’t parse it because it has another array inside. My JSON looks like that:

   "statementId": "1",
   "movements": [
         "id": 65,
         "date": "2019-02-05",
         "number": 32,
         "balance": -4.62,
         "purpose": "1"
         "id": 1,
         "date": "2019-02-05",
         "number": 22,
         "balance": -3,
         "purpose": "23"
         "id": 32,
         "date": "2019-02-05",
         "number": 12,
         "balance": -11,
         "purpose": "2"
   "startPointer": "1122",
   "endPointer": "3333"

I am using JsonSlurper. I want to know if it is possible to catch all the data inside “movements”, I have tried to use this script:

JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(bodyContent)
String parsed_movements = parsedJson["movements"]

I have no problem with parsing single strings, like statementId or startPointer, but when I try to parse movements with my script it gives me result as null. I have also tried parsedJson[“movements”][0] to catch first movement but it also gives me an error.

I have found a lot of things about json parsers on internet and also on stackoverflow but nothing what I seek. I really don’t think that it is a duplicate question.

EDIT: I tried for statement also to put each object in array like that:
def movements_array = []

for(def i = 0; i < parsedJson.movements.size(); i++) {
	movements_array << parsedJson.movements[i].id

But it gives me an error: Cannot invoke method size() on null object, because parsedJson.movements is null.

Good morning,

println parsedJson.get("movements").size()

works fine for me. It returns 3 as expected.
To loop through all objects inside your array, use this code:

for(int i = 0; i < parsedJson.get("movements").size(); i++) {
	println parsedJson.get("movements").get(i).get("id")

Hello Marek,
It gives me a following error.

Are you sure you are receiving correct API response? Because I used yours and it works without any problem.

Ohhhh, I was using it before getting body content. But now I use it correctly and parsedJson.get(“movements”).size() gives me 0 and for statement don’t works.

Still better than null. :slight_smile: Please check the response if “movements” array contains at least 1 object.

Thank you very much ! I really don’t know whats wrong with you. I had set wrong variables and it was giving me an empty movements. Thank you for helping me Marek, and sorry.

Below follow below tutorial we have discussed how to handle json arrays as well