[Solved] How to parse a JSON file? [closed]


Here is a Python program that performs the conversion you ask for. Copy this code into a file called, for example, “convert.py”. You can then run the program like so:

python convert.py my_existing_file.json my_new_file.txt

Here is the program:

import argparse
import json


# Get filenames from user
parser = argparse.ArgumentParser()
parser.add_argument(
    'input', type=argparse.FileType('r'), help="input JSON filename")
parser.add_argument(
    'output', type=argparse.FileType('w'), help="output 2-col text filename")
args = parser.parse_args()

# Read data in
data = json.load(args.input)

# Convert data to abstracted format
data = [[d["diagnoses"][0]["tumor_stage"], d["case_id"]] for d in data]

# Write the data out:
for d in data:
    args.output.write("{}\t{}\n".format(*d))

solved How to parse a JSON file? [closed]