Nested Json to Content Fragment Model | Community
Skip to main content
Prashardan
Level 4
April 23, 2024
Solved

Nested Json to Content Fragment Model

  • April 23, 2024
  • 3 replies
  • 961 views

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.

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Prashardan

Hi @estebanbustamante 

 

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

 

thanks for checking 

3 replies

sravs
Community Advisor
Community Advisor
April 23, 2024

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%20can%20parse%20text,value%20at%20the%20specified%20position.

 

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
April 26, 2024

@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
Prashardan
PrashardanAuthorAccepted solution
Level 4
April 26, 2024

Hi @estebanbustamante 

 

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

 

thanks for checking 

kautuk_sahni
Community Manager
Community Manager
May 16, 2024

@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