title: Convert json to csv with js
body: <p>I need to convert my JSON to a CSV file, but I don't know how to do it correctly.</p>
  
  <p>I have this code already</p>
  
  <pre class="lang-js prettyprint-override"><code>      let rows = typeof objArray !== "object" ? JSON.parse(objArray) : objArray;
        let header = "";
        Object.keys(rows[0]).map(pr =&gt; (header += pr + ";"));
  
        let str = "";
        rows.forEach(row =&gt; {
          let line = "";
          let columns =
            typeof row !== "object" ? JSON.parse(row) : Object.values(row);
  
          columns.forEach(column =&gt; {
            if (line !== "") {
              line += ";";
            }
            if (typeof column === "object") {
              line += JSON.stringify(column);
            } else {
              line += column;
            }
          });
          str += line + "\r\n";
        });
        return header + "\r\n" + str;
  </code></pre>
  
  <p>But I expect a different result then what I have now</p>
  
  <p><a href="https://i.stack.imgur.com/kwaYe.png" rel="nofollow noreferrer">wrong result</a></p>
  
  <p>I need this <a href="https://i.stack.imgur.com/k7d2C.png" rel="nofollow noreferrer">expected result</a></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  function ConvertToCSV(objArray) {
        let rows = typeof objArray !== "object" ? JSON.parse(objArray) : objArray;
        let header = "";
        Object.keys(rows[0]).map(pr =&gt; (header += pr + ";"));
  
        let str = "";
        rows.forEach(row =&gt; {
          let line = "";
          let columns =
            typeof row !== "object" ? JSON.parse(row) : Object.values(row);
  
          columns.forEach(column =&gt; {
            if (line !== "") {
              line += ";";
            }
            if (typeof column === "object") {
              line += JSON.stringify(column);
            } else {
              line += column;
            }
          });
          str += line + "\r\n";
        });
        return header + "\r\n" + str;
    }
    
    function downloadCSV () {
      let data = [{"am":{"a3":2,"a1":5,"a2":2},"zul":{"mer":1,"spi":1,"aut":1,"inf2":1,"int":1,"infl":1,"les":1},"kaab":      {"ka11":6,"ka12":6,"ka10":6},"kg3":"fdsf","ges":1,"alt":3,"kawe":{"ka9":4,"ka7":5,"ka8":5},"kaak":{"ka2":4,"ka3":5,"ka4":5,"ka1":4,"ka5":3,"ka6":5},"kg4":2,"kg1":5,"eink":"","kg2":4,"soz2":"","solz":"2","ae":{"a6":2,"a5":2,"a4":6}}]
      
      let result = ConvertToCSV(data);
      
      let fileToSave = new Blob([result], {
         type: "csv",
         name: 'data.csv
       });
  
       saveAs(fileToSave, 'data.csv);
    }
    </code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdn.jsdelivr.net/npm/file-saver@2.0.2/dist/FileSaver.min.js"&gt;&lt;/script&gt;
  &lt;button onclick="downloadCSV"&gt;download csv&lt;/button&gt;</code></pre>
  </div>
  </div>
  </p>