0

I am having a complex.json file and I want it to convert into CSV file using Java Spring boot.

Below is my complex.json file:

{
  "requestId": "1P02XV425KCASEZGQLB8LPQMF4ZXCURRR01",
  "recyleBinRecords": [
    {
      "Record": {
        "ProductName": "Name1",
        "Id": "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01"
        "ProductArea": "Area1"
      },
      "dependecyMaterial": [
        {
          "Name": "D1",
          "id": "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
          "parentid": "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
          "value": "Value1"
        },
        {
          "Name": "d2",
          "id": "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
          "parentid": "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
          "value": "value2"
        }]},{
      "Record": {
        "ProductName": "Name2",
        "Id": "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
        "ProductArea": "Product2"
      },
      "dependecyMaterial": [
        {
          "Name": "D3",
          "id": "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
          "parentid": "P1",
          "value": "value1"
        },
        {
          "Name": "D4",
          "id": "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01",
          "parentid": "P2",
          "value": "value2"
        }
      ]
    }
  ]
}

Below is code I tried but it's giving no column exception.

JsonNode jsonTree = new ObjectMapper().readTree(new File("src/main/resources/complex.json"));
Builder csvSchemaBuilder = CsvSchema.builder();
JsonNode firstObject = jsonTree.elements().next();
firstObject.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );
CsvSchema csvSchema = csvSchemaBuilder.build().withHeader();
CsvMapper csvMapper = new CsvMapper();
csvMapper.writerFor(JsonNode.class)
  .with(csvSchema)
  .writeValue(new File("src/main/resources/orderLines.csv"), jsonTree);Copy

Need a solution to convert my JSON to CSV in java.

1
  • 1
    Could you provide more error details (stack trace)? Also, you need a comma (,) on the 7th line (after "Id": "1P02XV425KCASEZGQLB8LPQMF4ZXCUBCTA01"). Commented Dec 19, 2022 at 18:22

1 Answer 1

1

There are a couple of things that are causing this. Main reason for the exception you are getting is that your csv schema does not have columns to map. Run a debugger and see what the line JsonNode firstObject = jsonTree.elements().next(); returns and what fields are added to the schema when you run firstObject.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );

Having said that, the parsing may still not go through as CSV generator does not support nested Objects. There are several ways to handle this. You can write a custom method to handle the structure and spit out flattened data or use a library to handle the transformation. One way that I have done it in the past is by using the JOLT library (json to json transform) to pre-process the JSON and create an interim document and parsing it. See details in this answer - Return a JSON response as CSV file from spring-boot controller

Sign up to request clarification or add additional context in comments.

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.