marjun
marjun

Reputation: 726

how to iterate json and convert into following json format in JAVA

How to iterate json in java. I wanted iterate elements inside payload, grab the first level and second level key and array value. I have converted the same in python. I'm new to java and wanted convert in java. Any help would be appreciated.

Python Code:

import os
import sys
import json
import datetime
def lambda_handler(event, context):
    indata = event['payload']
    outputDictionary = []
    for i in indata:
        for j in indata[i]:
            for k in indata[i][j]:
                outputDictionary.append(dict(source = i,code=j,version=k))
    return outputDictionary 

Input :

{
    "payload": {
        "gtl": {
            "435185": [
                "2019-11-27T14:34:32.368197Z"
            ]
        },
        "ptl": {
            "A0947863": [
                "2019-11-27T14:34:32.368197Z"
            ]
        },
        "mtl": {
            "A0947863": [
                "2019-11-27T14:34:32.368197Z",
                "2021-04-27T06:13:12.841968Z"
            ]
        }
    }
}

Expected Output

[
    {
        "source": "gtl",
        "code": "435185",
        "version": "2019-11-27T14:34:32.368197Z"
    },
    {
        "source": "ptl",
        "code": "A0947863",
        "version": "2019-11-27T14:34:32.368197Z"
    },
    {
        "source": "mtl",
        "code": "A0947863",
        "version": "2019-11-27T14:34:32.368197Z"
    },
    {
        "source": "mtl",
        "code": "A0947863",
        "version": "2021-04-27T06:13:12.841968Z"
    }
]

SOLVED IN JAVA: Java CODE:

import java.util.Iterator;

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


public class ParseJson {

    public static void main(String[] args) {

        String inputJson = "{\r\n" + 
                "   \"payload\": {\r\n" + 
                "       \"gtl\": {\r\n" + 
                "           \"435185\": [\r\n" + 
                "               \"2019-11-27T14:34:32.368197Z\"\r\n" + 
                "           ]\r\n" + 
                "       },\r\n" + 
                "       \"ptl\": {\r\n" + 
                "           \"A0947863\": [\r\n" + 
                "               \"2019-11-27T14:34:32.368197Z\"\r\n" + 
                "           ]\r\n" + 
                "       },\r\n" + 
                "       \"mtl\": {\r\n" + 
                "           \"A0947863\": [\r\n" + 
                "               \"2019-11-27T14:34:32.368197Z\",\r\n" + 
                "               \"2021-04-27T06:13:12.841968Z\"\r\n" + 
                "           ]\r\n" + 
                "       }\r\n" + 
                "   }\r\n" + 
                "}";
        final JSONObject inputJSONOBject = new JSONObject(inputJson);
        JSONObject jsonChildObject = (JSONObject)inputJSONOBject.get("payload");        
        
        Iterator iterator  = jsonChildObject.keys();
        while (iterator.hasNext()) {
            String key = null;
            while(iterator.hasNext()){
                String source = (String)iterator.next();
                JSONObject codeChildObject = jsonChildObject.getJSONObject(source); 
                Iterator iterator2  = codeChildObject.keys();
                while(iterator2.hasNext()){
                    String code = (String)iterator2.next();
                    org.json.JSONArray jsonArray = codeChildObject.getJSONArray(code);
                    for(int i=0;i<jsonArray.length();i++){
                        
                        System.out.println("Source:" + source);
                        System.out.println("CODE:" + code); 
                        System.out.println("Version: "+jsonArray.get(i));
                    }
                }
                }
            }
        }
        
        //System.out.println(inputJSONOBject);
            
    }

Upvotes: 0

Views: 155

Answers (1)

Arun Gowda
Arun Gowda

Reputation: 3500

You have done everything right except for that extra loop for the first iterator. Following is the complete working solution.

class ParseJson {

    public static void main(String[] args) throws Exception {

        String inputJson = "{\r\n" +
                "   \"payload\": {\r\n" +
                "       \"gtl\": {\r\n" +
                "           \"435185\": [\r\n" +
                "               \"2019-11-27T14:34:32.368197Z\"\r\n" +
                "           ]\r\n" +
                "       },\r\n" +
                "       \"ptl\": {\r\n" +
                "           \"A0947863\": [\r\n" +
                "               \"2019-11-27T14:34:32.368197Z\"\r\n" +
                "           ]\r\n" +
                "       },\r\n" +
                "       \"mtl\": {\r\n" +
                "           \"A0947863\": [\r\n" +
                "               \"2019-11-27T14:34:32.368197Z\",\r\n" +
                "               \"2021-04-27T06:13:12.841968Z\"\r\n" +
                "           ]\r\n" +
                "       }\r\n" +
                "   }\r\n" +
                "}";


        JSONArray output = convert(inputJson);
        System.out.println(output.toString(5));
    }

    private static JSONArray convert(String inputJson) throws Exception {
        final JSONObject inputJSONOBject = new JSONObject(inputJson);
        JSONObject jsonChildObject = (JSONObject) inputJSONOBject.get("payload");


        JSONArray outputArray = new JSONArray();


        Iterator payloadIterator = jsonChildObject.keys();//gtl,ptl,mtl
        while (payloadIterator.hasNext()) {
            String source = (String) payloadIterator.next(); //gtl

            JSONObject codeChildObject = jsonChildObject.getJSONObject(source);

            Iterator codeIterator = codeChildObject.keys();//123, 456

            while (codeIterator.hasNext()) {
                String code = (String) codeIterator.next();//123
                org.json.JSONArray jsonArray = codeChildObject.getJSONArray(code);// t1,t2,t3

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject object = new JSONObject();
                    object.put("source", source);
                    object.put("code", code);
                    object.put("version", jsonArray.get(i)); //t1
                    outputArray.put(object);
                }
            }
        }
        return outputArray;
    }
}

Upvotes: 1

Related Questions