title: How to flatten a nested JSON recursively, with flatten_json
body: <h3>This question is specific to using <code>flatten_json</code> from <a href="https://github.com/amirziai/flatten" rel="noreferrer">GitHub Repo: flatten</a></h3>
  <ul>
  <li>The package is on pypi <a href="https://pypi.org/project/flatten-json/" rel="noreferrer">flatten-json 0.1.7</a> and can be installed with <code>pip install flatten-json</code></li>
  <li>This question is specific to the following component of the package:</li>
  </ul>
  <pre class="lang-py prettyprint-override"><code>def flatten_json(nested_json: dict, exclude: list=[''], sep: str='_') -&gt; dict:
      &quot;&quot;&quot;
      Flatten a list of nested dicts.
      &quot;&quot;&quot;
      out = dict()
      def flatten(x: (list, dict, str), name: str='', exclude=exclude):
          if type(x) is dict:
              for a in x:
                  if a not in exclude:
                      flatten(x[a], f'{name}{a}{sep}')
          elif type(x) is list:
              i = 0
              for a in x:
                  flatten(a, f'{name}{i}{sep}')
                  i += 1
          else:
              out[name[:-1]] = x
  
      flatten(nested_json)
      return out
  </code></pre>
  <h3>Use recursion to flatten nested <code>dicts</code></h3>
  <ul>
  <li><a href="https://realpython.com/python-thinking-recursively/" rel="noreferrer">Thinking Recursively in Python</a></li>
  <li><a href="https://towardsdatascience.com/flattening-json-objects-in-python-f5343c794b10" rel="noreferrer">Flattening JSON objects in Python</a></li>
  </ul>
  <h3>How nested can <code>data</code> be?:</h3>
  <ul>
  <li><code>flatten_json</code> has been used to unpack a file that ended up being over 100000 columns</li>
  </ul>
  <h3>Can the flattened JSON, be unflattened?:</h3>
  <ul>
  <li>Yes, this question doesn't cover that.  However, if you install the <code>flatten</code> package, there is an <code>unflatten</code> method, but I haven't tested it.</li>
  </ul>