简体   繁体   中英

Search multiple JSON files and extract values based on some condition in Java

I have a scenario where there are multiple JSON files. I want to extract the values from all the JSON files matching the condition which I provide.

For Example:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

I have a condition to extract all the GlossEntryID which has "Acronym": "SGML"

I am very new to JSON so any lead would be appreciated.

try below code to parse json and check condition

try {
        String json = "{" +
                "    \"glossary\": {" +
                "        \"title\": \"example glossary\"," +
                "        \"GlossDiv\": {" +
                "            \"title\": \"S\"," +
                "            \"GlossList\": {" +
                "                \"GlossEntry\": {" +
                "                    \"ID\": \"SGML\"," +
                "                    \"SortAs\": \"SGML\"," +
                "                    \"GlossTerm\": \"Standard Generalized Markup Language\"," +
                "                    \"Acronym\": \"SGML\"," +
                "                    \"Abbrev\": \"ISO 8879:1986\"," +
                "                    \"GlossDef\": {" +
                "                        \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\"," +
                "                        \"GlossSeeAlso\": [\"GML\", \"XML\"]" +
                "                    }," +
                "                    \"GlossSee\": \"markup\"" +
                "                }" +
                "            }" +
                "        }" +
                "    }" +
                "}";

        // parse the whole JSON string into JSONObject
        JSONObject obj = new JSONObject(json);
        // retrieve glossary obj from JSON
        JSONObject glossary = obj.getJSONObject("glossary");
        // retrieve GlossDiv obj from glossary
        JSONObject GlossDiv = glossary.getJSONObject("GlossDiv");
        // retrieve GlossList obj from GlossDiv
        JSONObject GlossList = GlossDiv.getJSONObject("GlossList");
        // retrieve GlossEntry obj from GlossList
        JSONObject GlossEntry = GlossList.getJSONObject("GlossEntry");
        // Check condition
        if (GlossEntry.getString("Acronym").equalsIgnoreCase("SGML")) {
            String id = GlossEntry.getString("ID");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

If you are using Jayway JsonPath , you can simply achieve this as follows:

Maven Dependency

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency>

Code Snippet

DocumentContext jsonContext = JsonPath.parse(jsonStr);
List<String> idList = jsonContext.read("$.glossary.GlossDiv.GlossList.GlossEntry[?(@.Acronym == 'SGML')].ID");
System.out.println(idList.toString());

Console Output

["SGML"]

Then you can read those JSON files either sequentially or parallelly to combine these results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM