0

enter image description here

I am trying to parse below json, which has multiple nested lists (input & output), how do I parse it ?

{
  "status":"current",
  "entry":[{
      "item":{
        "name":"Task1",
        "identifier":"T00001",
        "input":[{
            "type":{
              "text":"InputTask1-1"
            },
            "valueString":"Weekly"
          },
          {
            "type":{
              "text":"InputTask1-2"
            },
            "valueString":"Daily"
          }
        ],
        "output":[{
            "type":{
              "text":"OutputTask1-1"
            },
            "valueString":"Daily"
          },
          {
            "type":{
              "text":"OutputTask1-2"
            },
            "valueString":"Weekly"
          }
        ]
      }
    },
    {
      "item":{
        "name":"Task2",
        "identifier":"T00002",
        "input":[{
            "type":{
              "text":"InputTask2-1"
            },
            "valueString":"Weekly"
          },
          {
            "type":{
              "text":"InputTask2-2"
            },
            "valueString":"Daily"
          }
        ],
        "output":[{
            "type":{
              "text":"OutputTask2-1"
            },
            "valueString":"Daily"
          },
          {
            "type":{
              "text":"OutputTask2-2"
            },
            "valueString":"Weekly"
          }
        ]
      }
    }
  ]
}

Looking for delimited output file like this:

"TaskName"|"TaskId"|"InputName"|"InputValue"|"OutputName"|"OutputValue"
Task1|T00001|InputTask1-1|Weekly|outputDummyText|outputDummyValue
Task1|T00001|InputTask1-2|Daily|outputDummyText|outputDummyValue
Task1|T00001|inputDummyText|inputDummyValue|OutputTask1-1|Daily
Task1|T00001|inputDummyText|inputDummyValue|OutputTask1-2|Weekly
Task2|T00002|InputTask2-1|Weekly|outputDummyText|outputDummyValue
Task2|T00002|InputTask2-2|Daily|outputDummyText|outputDummyValue
Task2|T00002|inputDummyText|inputDummyValue|OutputTask2-1|Daily
Task2|T00002|inputDummyText|inputDummyValue|OutputTask2-2|Weekly

2 Answers 2

2

Use Kotlin and Gson library.

Define your model:

data class MyData(val status: String, val entry: List<Entry.ItemContainer>) {

    data class Entry(val itemContainer: List<ItemContainer>) {

        data class ItemContainer(val item: Item) {

            data class Item(
                val name: String,
                val identifier: String,
                val input: List<TypeContainer>,
                val output: List<TypeContainer>
            ) {

                data class TypeContainer(val valueString: String, val type: Type) {

                    data class Type(val text: String)
                }
            }
        }

    }
}

Read json string from file:

val json = File("data.json").readText()

Map the data to the defined model using Gson:

val myData = Gson().fromJson<MyData>(json, MyData::class.java)

Print the result in desired format (or save it to file):

println("\"TaskName\"|\"TaskId\"|\"InputName\"|\"InputValue\"|\"OutputName\"|\"OutputValue\"")

for (itemContainer: MyData.Entry.ItemContainer in myData.entry) {
    with(itemContainer.item) {
        for (typeContainer in input) {
            println("$name|$identifier|${typeContainer.type.text}|${typeContainer.valueString}|outputDummyText|outputDummyValue")
        }

        for (typeContainer in output) {
            println("$name|$identifier|inputDummyText|inputDummyValue|${typeContainer.type.text}|${typeContainer.valueString}")
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

      JSONObject jsonParent = new JSONObject(response);
      JSONArray jsonarray = jsonParent .getJSONArray("entry");
        for (int i = 0; i < jsonarray.length(); i++) {
          JSONObject c = jsonarray.getJSONObject(i);
        
          JSONObject jsonItem = c.getString("item");
        
          JSONArray inputItemArray = jsonItem.getJSONArray("input");
        
            for (int j = 0; j < inputItemArray.length(); j++) {
              JSONObject inputItemObj = inputItemArray .getJSONObject(j);
              String invalueString = inputItemObj.getSting("valueString ");
            }
        
          JSONArray outputItemArray = jsonItem.getJSONArray("output");
        
          for (int k = 0; k < outputItemArray.length(); k++) {
            JSONObject outputItemObj = outputItemArray.getJSONObject(K);
            String outvalueString = outputItemObj.getSting("valueString ");
          }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.