title: How to flatten multilevel/nested JSON?
body: <p>I am trying to convert JSON to CSV file, that I can use for further analysis. Issue with my structure is that I have quite some nested dict/lists when I convert my JSON file.</p>
  <p>I tried to use pandas <code>json_normalize()</code>, but it only flattens first level.</p>
  <pre><code>import json
  import pandas as pd
  from pandas.io.json import json_normalize
  from cs import CloudStack
  
  api_key = xxxx
  secret = xxxx
  endpoint = xxxx
  
  cs = CloudStack(endpoint=endpoint,
                  key=api_key,
                  secret=secret)
  
  virtual_machines = cs.virtMach()
  
  test = json_normalize(virtual_machines[&quot;virtualmachine&quot;])
  
  test.to_csv(&quot;test.csv&quot;, sep=&quot;|&quot;, index=False)
  </code></pre>
  <p>Any idea how to flatter whole JSON file, so I can create single line input to CSV file for single (in this case virtual machine) entry? I have tried couple of solutions posted here, but my result was always only first level was flattened.</p>
  <p>This is sample JSON (in this case, I still get &quot;securitygroup&quot; and &quot;nic&quot; output as JSON format:</p>
  <pre><code>{
      &quot;count&quot;: 13,
      &quot;virtualmachine&quot;: [
          {
              &quot;id&quot;: &quot;1082e2ed-ff66-40b1-a41b-26061afd4a0b&quot;,
              &quot;name&quot;: &quot;test-2&quot;,
              &quot;displayname&quot;: &quot;test-2&quot;,
              &quot;securitygroup&quot;: [
                  {
                      &quot;id&quot;: &quot;9e649fbc-3e64-4395-9629-5e1215b34e58&quot;,
                      &quot;name&quot;: &quot;test&quot;,
                      &quot;tags&quot;: []
                  }
              ],
              &quot;nic&quot;: [
                  {
                      &quot;id&quot;: &quot;79568b14-b377-4d4f-b024-87dc22492b8e&quot;,
                      &quot;networkid&quot;: &quot;05c0e278-7ab4-4a6d-aa9c-3158620b6471&quot;
                  },
                  {
                      &quot;id&quot;: &quot;3d7f2818-1f19-46e7-aa98-956526c5b1ad&quot;,
                      &quot;networkid&quot;: &quot;b4648cfd-0795-43fc-9e50-6ee9ddefc5bd&quot;
                      &quot;traffictype&quot;: &quot;Guest&quot;
                  }
              ],
              &quot;hypervisor&quot;: &quot;KVM&quot;,
              &quot;affinitygroup&quot;: [],
              &quot;isdynamicallyscalable&quot;: false
          }
      ]
  }
  </code></pre>