title: How to generated the csv file from json data with Java?
body: <p>I try to generate csv file from json type data. These are my json test data.</p>
  <pre><code>{
    &quot;realtime_start&quot;:&quot;2020-09-25&quot;,
    &quot;realtime_end&quot;:&quot;2020-09-25&quot;,,
    &quot;units&quot;: &quot;Percent&quot;,
    &quot;seriess&quot;: [
      {
        &quot;name&quot;: &quot;James&quot;,
        &quot;age&quot;: 29,
        &quot;house&quot;: &quot;CA&quot;
      },
      {
        &quot;name&quot;: &quot;Jina&quot;,
        &quot;age&quot;: 39,
        &quot;house&quot;: &quot;MA&quot;,
        &quot;notes&quot;: &quot;Million tonne punch&quot;
      },
  }
  </code></pre>
  <p>The problem is json array type <code>&quot;seriess</code>&quot; does not contain <code>&quot;notes&quot;</code> node in all every nodes.
  I made the below java codes to change this json data to csv file with header columns</p>
  <pre><code>JSONObject json = getJsonFileFromURL(...)
  
  JSONArray docsArray = json.getJSONArray(&quot;seriess&quot;);
  docsArray.put(json.get(&quot;realtime_start&quot;));
  docsArray.put(json.get(&quot;realtime_end&quot;));
  docsArray.put(json.get(&quot;units&quot;));
  
  JsonNode jsonTree = new ObjectMapper().readTree(docsArray.toString());
          
  Builder csvSchemaBuilder = CsvSchema.builder();
  for(JsonNode node : jsonTree) {
      node.fieldNames().forEachRemaining(fieldName -&gt; {csvSchemaBuilder.addColumn(fieldName);} );
  }
  
  CsvSchema csvSchema = csvSchemaBuilder.build().withHeader();
  CsvMapper csvMapper = new CsvMapper();
  csvMapper.writerFor(JsonNode.class).with(csvSchema).writeValue(new File(&quot;test.csv&quot;), jsonTree);
  </code></pre>
  <p>But the incorrect results are shown like below,</p>
  <pre><code>realtime_start,realtime_end,units,names,age,house,realtime_start,realtime_end,units,names,age,house,notes, realtime_start,.....
  </code></pre>
  <p>The generated header columns does not contain distinct values. The header columns are added in duplicate. How can I generate the distinct header like below</p>
  <pre><code>realtime_start,realtime_end,units,names,age,house, notes
  </code></pre>
  <p>Any idea?</p>
  <h2>Update Part</h2>
  <p>I try to extract data from the FRED (FEDERAL RESERVE BANK OF ST. LOUIS). FRED provide simple and convenient Python api like below,</p>
  <pre><code>from fredapi import Fred 
  import pandas as pd
  
  fred = Fred(api_key='abcdefghijklmnopqrstuvwxyz0123456789')
  data_unemploy = fred.search('Unemployment Rate in California')
  data_unemploy.to_csv(&quot;test_unemploy.csv&quot;)
  </code></pre>
  <p>But the java apis are deprecated, so I have to develop simple Java api which convert json values to csv file. I found the below Java codes with googling</p>
  <pre><code>JSONObject json = getJsonFileFromURL(&quot;https://api.stlouisfed.org/fred/series/search?search_text=Unemployment+Rate+in+California&amp;api_key=abcdefghijklmnopqrstuvwxyz0123456789&amp;file_type=json&quot;);
          
  JSONArray docsArray = json.getJSONArray(&quot;seriess&quot;);
  docsArray.put(json.get(&quot;realtime_start&quot;));
  docsArray.put(json.get(&quot;realtime_end&quot;));
  
  JsonNode jsonTree = new ObjectMapper().readTree(docsArray.toString());
  JsonNode firstObject = jsonTree.elements().next();  // I am struggling with this line 
  firstObject.fieldNames().forEachRemaining(fieldName -&gt; {csvSchemaBuilder.addColumn(fieldName);} );
  CsvSchema csvSchema = csvSchemaBuilder.build().withHeader();
          
  CsvMapper csvMapper = new CsvMapper();
  csvMapper.writerFor(JsonNode.class).with(csvSchema).writeValue(new File(&quot;test.csv&quot;), jsonTree);
  </code></pre>
  <p>To extract columns from json data <code>JsonNode firstObject = jsonTree.elements().next();</code> return the first json node. But this line does not return <code>notes</code> column. because the first line does not contain the <code>notes</code> key value.</p>
  <p>So I change this code line  to following lines</p>
  <pre><code>for(JsonNode node : jsonTree) {
      node.fieldNames().forEachRemaining(fieldName -&gt; {
          csvSchemaBuilder.addColumn(fieldName);
      } );
  }
  </code></pre>
  <p>But these lines throws the results which I do not expect. The repeated duplicated columns like below</p>
  <pre><code>realtime_start,realtime_end,units,names,age,house,realtime_start,realtime_end,units,names,age,house,notes, realtime_start,.....
  </code></pre>
  <p>I am totally stuck with this part.</p>