title: Nested Json file to csv using Python
body: <p>I am new to python and need help in converting a nested json file to csv.
  File name: users.json
  file path: C:/_apps/test</p>
  <hr />
  <p>File Content:</p>
  <pre><code>{
      &quot;count&quot;:  3,
      &quot;next&quot;:  null,
      &quot;previous&quot;:  null,
      &quot;results&quot;:  [
                      {
                          &quot;user&quot;: &quot;@{id=1111; email=pqr@abc.com; first_name=Ann; last_name=pqr}&quot;,
                          &quot;active&quot;:  true,
                          &quot;marketing_optin&quot;:  null,
                          &quot;enrolled_at&quot;:  &quot;2018-05-08T18:22:15.125868Z&quot;,
                          &quot;expires_at&quot;:  null,
                          &quot;access_code&quot;:  null
                      },
                      {
                          &quot;user&quot;:  &quot;@{id=2222; email=xyz@abc.com; first_name=Benn; last_name=xyz}&quot;,
                          &quot;active&quot;:  true,
                          &quot;marketing_optin&quot;:  null,
                          &quot;enrolled_at&quot;:  &quot;2018-05-08T18:41:46.905016Z&quot;,
                          &quot;expires_at&quot;:  null,
                          &quot;access_code&quot;:  null
                      },
                      {
                          &quot;user&quot;:  &quot;@{id=3333; email=rst@abc.com; first_name=Cathy; last_name=rst}&quot;,
                          &quot;active&quot;:  true,
                          &quot;marketing_optin&quot;:  null,
                          &quot;enrolled_at&quot;:  &quot;2018-05-08T18:41:47.015329Z&quot;,
                          &quot;expires_at&quot;:  null,
                          &quot;access_code&quot;:  null
                      }
                  ]
  }
  </code></pre>
  <p>The output file format should be as shown below:</p>
  <pre><code>user_id,user_email,user_first_name,user_last_name,active,marketing_optin,enrolled_at,expires_at,access_code
  </code></pre>
  <p>So, far I tried with different methods and none of them gave the required result.
  I was not even able to read the json file.
  I tried below code :</p>
  <pre><code>with open('C:\\_apps\\users.json') as data_file:
    data = data_file.read()
    data_content = json.loads(data)
  </code></pre>
  <p>But is it throwing the below error:</p>
  <pre><code>    raise JSONDecodeError(&quot;Expecting value&quot;, s, err.value) from None
  json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  </code></pre>
  <p>Posting the code below which currently I am trying with:</p>
  <pre><code>import json
  from pandas.io.json import json_normalize
  import argparse
  
  
  def flatten_json(y):
      out = {}
  
      def flatten(x, user=''):
          if type(x) is dict:
              for a in x:
                  flatten(x[a], user + a + '_')
          elif type(x) is list:
              i = 0
              for a in x:
                  flatten(a, user + str(i) + '_')
                  i += 1
          else:
              out[user[:-1]] = x
      flatten(y)
      return out
  
  
  if __name__ == '__main__':
      parser = argparse.ArgumentParser(description='Converting json files into csv')
      parser.add_argument(
          &quot;-j&quot;, &quot;--json&quot;, dest=&quot;users.json&quot;, help=&quot;C:\_apps\training\output\&quot;, metavar=&quot;FILE&quot;, required=True)
      print(parser)
      args = parser.parse_args()
      print(args)
  
      with open(args.json_file, &quot;r&quot;) as inputFile:  # open json file
          json_data = json.loads(inputFile.read())  # load json content
      final_data = pd.DataFrame([flatten_json(elt) for elt in json_data['results']])
  
      with open(args.json_file.replace(&quot;.json&quot;, &quot;.csv&quot;), &quot;w&quot;) as outputFile:  # open csv file
  
          # saving DataFrame to csv
          final_data.to_csv(outputFile, encoding='utf8', index=False)
  </code></pre>
  <p>And it throws an exception at</p>
  <pre><code> args = parser.parse_args()
  </code></pre>