How to Parse and Export Multilevel JSON into a CSV File

Question
I’m trying to export JSON to a csv file, but I can’t seem to find a step by step tutorial. I’m wondering if anyone can help me?

I have parsed my JSON file using the approach described in this link.

It works now, but I still need to figure out how to export the parsed data into a CSV file..

You can find in the link below the JSON file I parsed, I just have to figure out how to export:

“attributeName”: “Description”,
“attributeValue”: ""
“attributeName”: “Functional Area”,
“attributeValue”: “TECH”
public static void main(String[] args){
 String json=“{\“content\”: [ {\“a\”:{ \“b\” : \“abcd\”, \“c\” : \“bcd\”},\“ab\” : \“123\”,\“abc\”:{\“id\” : \“12345\”, \“name\” : \“abcde\”},\“cd\”: \“afsf\”},{\“a\”:{\“b\” : \“abcd\”,  \“c\” : \“bcd\”},\“ab\” : \“123\”,\“abc\”:{\“id\” : \“12346\”,\“name\” : \“abcde\”},\“cd\”: \“afsf\”}]}”;
    JSONObject jsonObject = new JSONObject(json);
    JSONArray jsonArray = jsonObject.getJSONArray(“content”);
    for (int i = 0; i < jsonArray.length(); i++) {

JSONObject objects = jsonArray.getJSONObject(i);

String[] elementNames = JSONObject.getNames(objects);

for (String elementName : elementNames)

{

if(elementName.equalsIgnoreCase(“abc”)){

JSONObject value = objects.getJSONObject(elementName);

String[] elementList = JSONObject.getNames(value);

for(String j:elementList){

if(j.equalsIgnoreCase(“id”)){

System.out.println(value.getString(“id”));  

}

}

}

}
}

}

Output:-
12345
12346

Answer
To retrieve the id\name fields from the multilevel JSON data, your approach is too complicated. The following SPL script parses JSON, retrieves id\name fields and exports the parsed data to a CSV file. It does all things step by step, and it is simple enough.

A

1

=file(“d:\\source.json”).read()

2

=json(A1).content.(abc)

3

=file(“d:\\result.csv”).export@c(A2)

 

The SPL code can be easily integrated with a Java application.

Here’s the result of executing the SPL script:

A1: Read in JSON data as a string;

A2: Generate a table sequence based on the abc level.

A3: Export data in the table sequence to a CSV file.