JSON to CSV flattening nested JSON : <p>I am trying to Flatten JSON to parse as a CSV. But the flattening is not properly flattening. When I get the json to flatten customer.addresses is filling with addresstype:r then skipping all fields city,countrycode,countycode etc. and then starting at customer.companyName. The nested JSON is not breaking up properly to show properly in excel I think my JavaScript code must be off just a little bit. Any help with this would be greatly appreciated.</p>

<p><strong>JSON (this is a portion of the nested json it will not always be in the same depth is there a way to code for any type of nested json that will read at all levels)</strong></p>

<pre><code>[
  {
    "countyCode": 12,
    "customer": {
      "addresses": [
        {
          "addressType": "R",
          "city": "BRADENTON",
          "countryCode": "US",
          "countyCode": 12,
          "foreignPostalCode": null,
          "state": "FL",
          "streetAddress": "819 15th Ave Dr E",
          "zipCode": 34211,
          "zipPlus": null
        },
        {
          "addressType": "M",
          "city": "BRADENTON",
          "countryCode": "US",
          "countyCode": 12,
          "foreignPostalCode": null,
          "state": "FL",
          "streetAddress": "PO BOX 124",
          "zipCode": 34201,
          "zipPlus": 0124
        }
      ],
      "companyName": null,
      "customerNumber": 932874,
      "customerStopFlag": false,
      "customerType": "I",
      "dateOfBirth": "1936-08-05T00:00:00",
      "dlExpirationDate": "2022-08-05T00:00:00",
      "dlRenewalEligibilityFlag": true,
      "driverLicenseNumber": "B360722339284",
      "emailAddress": null,
      "feidNumber": null,
      "firstName": "David",
      "lastName": "Brierton",
      "middleName": "Hugh",
      "militaryExemptionFlag": null,
      "nameSuffix": null,
      "sex": "M"
</code></pre>

<p><strong>JS</strong></p>

<pre><code>function flatObjectToString(obj) {
    var s = "";
    Object.keys(obj).map(key =&gt; {
      if (obj[key] === null) {
        s += key + ":";
      } else if (obj[key].toLocaleDateString) {
        s += key + ": " + obj[key].toLocaleDateString() + "\n";
      } else if (obj[key] instanceof Array) {
        s += key + ":\n" + listToFlatString(obj[key]);
      } else if (typeof obj[key] == "object") {
        s += key + ":\n" + flatObjectToString(obj[key]);
      } else {
        s += key + ":" + obj[key];
      }
      s += "\n";
    });
    return s;
  }

  function listToFlatString(list) {
    var s = "";
    list.map(item =&gt; {
      Object.keys(item).map(key =&gt; {
        s += "";
        if (item[key] instanceof Array) {
          s += key + "\n" + listToFlatString(item[key]);
        } else if (typeof item[key] == "object" &amp;&amp; item[key] !== null) {
          s += key + ": " + flatObjectToString(item[key]);
        } else {
          s += key + ": " + (item[key] === null ? "" : item[key].toLocaleDateString ? item[key].toLocaleDateString : item[key].toString());
        }
        s += "\n";
      });
    });
    return s;
  }

  function flatten(object, addToList, prefix) {
    Object.keys(object).map(key =&gt; {
      if (object[key] === null) {
        addToList[prefix + key] = "";
      } else
      if (object[key] instanceof Array) {
        addToList[prefix + key] = listToFlatString(object[key]);
      } else if (typeof object[key] == 'object' &amp;&amp; !object[key].toLocaleDateString) {
        flatten(object[key], addToList, prefix + key + '.');
      } else {
        addToList[prefix + key] = object[key];
      }
    });
    return addToList;
  }
</code></pre>

<p>Then I run it through the Javascript Utilities with this:</p>

<pre><code>// Run the JSON string through the flattening utilities above
          var flatJSON = JSON.parse(evt.target.result).map(record =&gt; flatten(record, {}, ''));

          var csv = Papa.unparse(flatJSON);
</code></pre>
