Search code examples
pythonjsonexport-to-csv

Write a CSV from JSON, importing only given keys


I have JSONs reporting different values, and I want to import only some keys in a csv. I have tried 2 approaches, but both give me some problems. At first, I have tried this :

 `import os,json
    import glob
    import csv
    
    # Place your JSON data in a directory named 'data/'
    src = "MYPATH"
    data = []
    
    
    json_pattern = os.path.join(src, '*.json')
    # only json
    files = glob.glob(json_pattern, recursive=True)
    
    
    # Loop through files
    for single_file in files:
      with open(single_file, 'r') as f:
        json_file = json.load(f)
      try:
          data.append([
              json_file['name1'],
              json_file['name2'],
              json_file['name3'],
              json_file['name4'],
    
          ])
      except KeyError:
          continue
# Add headers
data.insert(0, ['title_1', 'title_2', 'title_3'])

# Export to CSV.
# Add the date to the file name to avoid overwriting it each time.
csv_filename = 'name.csv'
with open((src + csv_filename), "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(data)`

In this way, unfortunately, if a key is not included, the code skip the file altogether, while I want it to skip only the key. So I tried this, instead:

import os,json
import glob
import csv

# Place your JSON data in a directory named 'data/'
src = "MY_PATH"
data = []

json_pattern = os.path.join(src, '*.json')
# Change the glob if you want to only look through files with specific names
files = glob.glob(json_pattern, recursive=True)


# Loop through files
col_name = ['name1','name2','name4']
for single_file in files:
  with open(single_file, 'r') as f:
    json_file = json.load(f)
    for key in col_name:
      try:
        data.append([json_file[key]])
      except KeyError:
        continue



# Add headers
data.insert(0, ['title_1', 'title_2', 'title_3'])

# Export to CSV.
# Add the date to the file name to avoid overwriting it each time.
csv_filename = 'name.csv'
with open((src + csv_filename), "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(data)

But in this case, each value is a new row in the csv, while I want the value from each json in a single row.

I am not an expert and I really don't know how to combine this two. Can someone help me out? Thanks!


Solution

  • If I understand what you're trying to do correctly, why not just do

    # Loop through files
    for single_file in files:
      with open(single_file, 'r') as f:
        json_file = json.load(f)
        data.append([
          json_file.get('name1', ''),
          json_file.get('name2', ''),
          json_file.get('name3', ''),
          json_file.get('name4', '')
        ])
    

    By using .get() you can specify the default value in case a key isn't found.