Last Updated: 09/09/2022

How to write your data to a JSON file

There are several ways to write data to a JSON file

  • Load the JSON library for your programming language and use it to write out your data. (strongly recommended)
  • Manually write out the JSON file (either by print or write statements)
  • Download and use the CCMC-created JSON helper script

Load the JSON library for your programming language and use it to write out your data

The best way to write your data to a JSON file is to load the JSON library for your language and use it to write out a JSON file. This is the best method to ensure that the JSON is properly formatted.

Here is an example from https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/ on how to use Python's JSON library:

import json

data = {}
data['people'] = []
data['people'].append({
    'Scott',
    'website': 'stackabuse.com',
    'from': 'Nebraska'
})
data['people'].append({
    'name': 'Larry',
    'website': 'google.com',
    'from': 'Michigan'
})
data['people'].append({
    'name': 'Tim',
    'website': 'apple.com',
    'from': 'Alabama'
})

with open('data.txt', 'w') as outfile:
    json.dump(data, outfile)

If you use a different language, you can google ' write json file' and get the steps to do that in your programming language.

Download and use the CCMC-created Python Script

You can download the helper script (sep_json_writer.py) here (last updated on 2021.08.11).

Visit this page to see the ways to use this helper script.

Validating your JSON file

If you google 'json online validator', you can access validators to ensure that your data file is using proper JSON syntax.