How to iterate through json file in Java

Stack Overflow Asked by FranceMadrid on January 1, 2022

In my Json file I have the following field:

"categories": [{
  "name1": "Corporate",
  "parent": {
      "name_parent": "PlanType1"

In my Java file I have this code to access that json variable:


The .getCategories() is accessing the "categories" json variable, I just am having trouble iterating through "categories"

In my code I need the logic that if in "categories" if name_parent = "PlanType1" AND name1 = "Corporate" do x. I am just having trouble constructing that if statement by iterating through the json.

2 Answers

Try this. I don't actually know how the JSON variable you are getting. But it would work if u cast it into a List of Maps. For example

List<Map<String, Object>> catogoriesList = (List<Map<String, Object>>) PlanType.getnewEntity().getCategories(); 

Then you'll be able to iterate through it like as follows.

for(Map<String, Object> catogory : catogoriesList){
  Map<String, Object> parent = (Map<String, Object>) catogory.get("parent");
  if(catogory.get("name1").toString().equals("Corporate") && parent.get("name_parent").toString().equals("PlanType1")){
    // Do x here

Answered by Althaf1467 on January 1, 2022

You can iterate like below

                category -> {
                    if("Corporate".equals(category.getName1()) &&
                            ("PlanType1".equals(category.getParent().getNameParent()))) {

                        //do the logic

Answered by mgeor on January 1, 2022

Add your own answers!

Related Questions

How to store a list of objects in sqflite?

1  Asked on January 17, 2021 by 0x4b50


SQL to allocate rows from one table to another

3  Asked on January 17, 2021 by user3093820


JavaScript LocaleDateString back to Date Object

1  Asked on January 16, 2021 by victor-molina


Python global variables unexpected behavior

3  Asked on January 16, 2021 by user118967


Spying module and mocking module function

3  Asked on January 16, 2021 by ninita


Palindrome Checker for strings in C

1  Asked on January 16, 2021 by bradley-felix


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP