1

I would like to read the data from the JSON file, but not only the values but also the fields in this file, i.e. "_id", "name", "surname" etc. For example, I have a file like the one below and the problem is that the files will contain different data and it is not one and the same the file itself so the fields will change and not be the same all the time.

[
    {
        "_id": 1,
        "name": "Adam",
        "surname": "Smith",
        "course": "IT",
        "grades": [
            {
                "maths": 4,
                "physics": 4,
                "programming": 5
            },
            {
                "maths": 3,
                "physics": 5,
                "programming": 4
            }
        ]
    },
    {
        "_id": 2,
        "name": "Robert",
        "surname": "Brown",
        "course": "IT",
        "grades": [
            {
                "maths": 5,
                "physics": 5,
                "angielski": 5
            },
            {
                "maths": 4,
                "physics": 4,
                "programming": 4
            }
        ]
    }
]

I thought about parsing the file into a string and reading it character by character, but that would be time consuming. And here is my question, how to read not only values but also fields in a JSON file.

jamesnet214
  • 1,044
  • 13
  • 21
Piotror
  • 85
  • 1
  • 12

1 Answers1

2

I think you want to iterate over all Objects/Arrays in this json. If you are fine using a library, below one will be helpful

Maven:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>org.json</artifactId>
    <version>chargebee-1.0</version>
</dependency>

This is a sample code for reading your string

import java.io.IOException;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Test {

    public static void main(String[] args) throws IOException, JSONException {

        String json_str = "[{\r\n" + 
                "  \"_id\": 1,\r\n" + 
                "  \"name\": \"Adam\",\r\n" + 
                "  \"surname\": \"Smith\",\r\n" + 
                "  \"course\": \"IT\",\r\n" + 
                "  \"grades\": [\r\n" + 
                "    {\r\n" + 
                "     \"maths\": 4,\r\n" + 
                "     \"physics\": 4,\r\n" + 
                "     \"programming\": 5\r\n" + 
                "    },\r\n" + 
                "    {\r\n" + 
                "     \"maths\": 3,\r\n" + 
                "     \"physics\": 5,\r\n" + 
                "     \"programming\": 4\r\n" + 
                "    }]\r\n" + 
                "  },{\r\n" + 
                "  \"_id\": 2,\r\n" + 
                "  \"name\": \"Robert\",\r\n" + 
                "  \"surname\": \"Brown\",\r\n" + 
                "  \"course\": \"IT\",\r\n" + 
                "  \"grades\": [\r\n" + 
                "    {\r\n" + 
                "     \"maths\": 5,\r\n" + 
                "     \"physics\": 5,\r\n" + 
                "     \"angielski\": 5\r\n" + 
                "    },\r\n" + 
                "    {\r\n" + 
                "     \"maths\": 4,\r\n" + 
                "     \"physics\": 4,\r\n" + 
                "     \"programming\": 4\r\n" + 
                "    }]\r\n" + 
                "}]";

        JSONArray jsonArray = new JSONArray(json_str);
        int length = jsonArray.length();
        
        for(int i=0; i<length; i++) {
            
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            
            Iterator<String> keys = jsonObject.keys();

            while(keys.hasNext()) {
                
                //this will give 1st level keys - "surname,name,course,_id,grades"
                String key = keys.next();

                if (jsonObject.get(key) instanceof JSONObject) {
                    //build either a recursive function or required logic to iterate over inner json objects similar to this
                } else if (jsonObject.get(key) instanceof JSONArray) {
                    //build either a recursive function or required logic to iterate over inner json arrays similar to this
                } else {
                    /* Output:
                    key is = surname ==> value is = Smith
                    key is = name ==> value is = Adam
                    key is = course ==> value is = IT
                    key is = _id ==> value is = 1
                    key is = surname ==> value is = Brown
                    key is = name ==> value is = Robert
                    key is = course ==> value is = IT
                    key is = _id ==> value is = 2
                   */
                    System.out.println("key is = "+key+" ==> value is = "+jsonObject.get(key));
                }
                
            }
            
        }
        
    }

}
Chandan
  • 640
  • 4
  • 10