How to extract JSON response through Groovy script?

I have a Groovy script as shown below (Line#A in which I am getting the following JSON output

Line#A

println("onTime programs_o item component at line#305 "+onTime.item.component)

The above Groovy script prints the following JSON response:

onTime programs_o item component at line#205

[title_en_t:PrimeTime Politics, thumbnail_o:[item:[include:/site/components/thumbnails/programs/0a9818605810.xml, component:[createdDate_dt:2021-01-22T14:46:28.787Z, lastModifiedDate:2021-03-06T02:22:15.111Z, image_en_s:/static-assets/images/thumbnails/2021/03/05/time_thumbnail_en.jpg, image_fr_s:/static-assets/images/thumbnails/2021/03/05/time_thumbnail_fr.jpg], value:Time World, disableFlattening:false, key:/site/components/thumbnails/programs/7ad45a66-ba35-f46e-2ce9-0a9818605810.xml]], program_id_i:16]

What I want to achieve is I want to extract image_en_s and image_fr_s from the response above but I am unable to do so.

This is what I have tried:

// Line#B

println("onTime programs_o item component at line#305 "+onTime.item.component.image_en_s)

// Line#C

println("onTime programs_o item component at line#305 "+onTime.item.component.image_fr_s)

Problem Statement:

I am wondeirng what changes I need to make in the code above at Line#B and Line#C so that it extracts image_en_s and image_fr_s from the JSON response.


Solution 1:

1/ print it as pretty json to understand structure:

println("onTime programs_o item component at line#305 " +
    new groovy.json.JsonBuilder(onTime.item.component).toPrettyString())

this should give you understanding of nested structure

2/ according what you have in "line a" you should use this accessor:

onTime.item.component.thumbnail_o.item.component.image_fr_s