title: Python & Pandas: Flattening nested json with pd.json_normalize
body: <p>New to Python and Pandas, working on getting the hang of jsons. Any help appreciated.</p>
  <p>Via an API I'm pulling a nested json. The structure of the json is below. The fields I'm after are in <code>view</code>, labeled <code>user_id</code> and <code>message</code>, and then under the nested field <code>replies</code> the subfields <code>user_id</code> and <code>message</code>. The desired fields are labeled below with &lt;&lt;&lt;</p>
  <pre><code>  ],
    &quot;view&quot;: [
      {
        &quot;id&quot;: 109205,
        &quot;user_id&quot;: 6354, # &lt;&lt;&lt;&lt; this field
        &quot;parent_id&quot;: null,
        &quot;created_at&quot;: &quot;2020-11-03T23:32:49Z&quot;,
        &quot;updated_at&quot;: &quot;2020-11-03T23:32:49Z&quot;,
        &quot;rating_count&quot;: null,
        &quot;rating_sum&quot;: null,
        &quot;message&quot;: &quot;message text1&quot;, # &lt;&lt;&lt; this field
        &quot;replies&quot;: [
          {
            &quot;id&quot;: 109298,
            &quot;user_id&quot;: 5457, # &lt;&lt;&lt; this field
            &quot;parent_id&quot;: 109205,
            &quot;created_at&quot;: &quot;2020-11-04T19:42:59Z&quot;,
            &quot;updated_at&quot;: &quot;2020-11-04T19:42:59Z&quot;,
            &quot;rating_count&quot;: null,
            &quot;rating_sum&quot;: null,
            &quot;message&quot;: &quot;message text2&quot; # &lt;&lt;&lt; this field
          },
          {
           #json continues
  </code></pre>
  <p>I can successfully pull the top level fields under <code>view</code>, but I'm having difficulty flattening the nested json field <code>replies</code> with <code>json_normalize</code>. Here's my working code:</p>
  <pre><code>import pandas as pd
  
  d = r.json() # json pulled from API
  
  df = pd.json_normalize(d['view'], record_path=['replies'])
  
  print(df)
  </code></pre>
  <p>Which results in the following KeyError:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\danie\AppData\Local\Temp\atom_script_tempfiles\2021720-13268-1xuqx61.3oh2g&quot;, line 53, in &lt;module&gt;
      df = pd.json_normalize(d['view'], record_path=['replies'])
    File &quot;C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\json\_normalize.py&quot;, line 336, in _json_normalize
      _recursive_extract(data, record_path, {}, level=0)
    File &quot;C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\json\_normalize.py&quot;, line 309, in _recursive_extract
      recs = _pull_records(obj, path[0])
    File &quot;C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\json\_normalize.py&quot;, line 248, in _pull_records
      result = _pull_field(js, spec)
    File &quot;C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\json\_normalize.py&quot;, line 239, in _pull_field
      result = result[spec]
  KeyError: 'replies'
  </code></pre>
  <p>What am I missing here? All suggestions welcome and appreciated.</p>