Expand my Community achievements bar.

SOLVED

Nested Json to Content Fragment Model

Avatar

Level 4

Hi Team

 

I have a requirement to read the nested json and add nested json values in dropdowns in Content Fragment Model.

 

For a normal json, I have created a servlet and calling the servlet to retrieve values to the dropdown in Content Fragment Model.

 

However my input is nested json and I want values in related dropdowns like based on the country selection I want states to be populated and based states selection I want cities to be populated.

 

Below is the example json.

{
"countries": [
{
"name": "United Kingdom",
"states": [
{
"name": "England",
"cities": ["London", "Manchester", "Birmingham"]
},
{
"name": "Scotland",
"cities": ["Glasgow", "Edinburgh", "Aberdeen"]
}
]
},
{
"name": "Australia",
"states": [
{
"name": "New South Wales",
"cities": ["Sydney", "Newcastle", "Wollongong"]
},
{
"name": "Victoria",
"cities": ["Melbourne", "Geelong", "Ballarat"]
}
]
},
{
"name": "Germany",
"states": [
{
"name": "Bavaria",
"cities": ["Munich", "Nuremberg", "Augsburg"]
},
{
"name": "North Rhine-Westphalia",
"cities": ["Cologne", "Düsseldorf", "Dortmund"]
}
]
}
]
}

 

Please advise.

 

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi @EstebanBustamante 

 

I have achieved the requirement without the need java. I will prove detailed steps here.

 

thanks for checking 

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @Prashardan,

 

You can update your servlet to accept request params as country/state.

 

if you are calling servlet for the country dropdown, pass the country value in request param, and create a method to get the states from the nested json depending upon the country value.

And implement the same for state dropdown also. Please refer on reading the values from nested json
https://www.tutorialspoint.com/how-can-we-parse-a-nested-json-object-in-java#:~:text=A%20JSONArray%2...

 

Avatar

Community Advisor

@Prashardan Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community



Esteban Bustamante

Avatar

Correct answer by
Level 4

Hi @EstebanBustamante 

 

I have achieved the requirement without the need java. I will prove detailed steps here.

 

thanks for checking 

Avatar

Administrator

@Prashardan Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni