I created an example using three scenarios.
The first is a Python script that creates a list and then writes it to a JSON file. Then we have a Perl script that reads in JSON, modifies it (adds three more elements to the array), and then writes it back to the JSON data file. The last script, in Python, shows how to read in JSON and use data.
Python script, create a list, write it to a json file
import json data = [1, 2, 3] with open('data.json', 'w') as jsonfile: json.dump(data, jsonfile)
Now the data file is as follows:
[1, 2, 3]
Perl script, reads the JSON file, flushes the data and writes it back:
use warnings; use strict; use JSON; my $file = 'data.json';
The data file now looks like this:
[1, 2, 3, 4, 5, 6]
Python script, reads the updated JSON file and converts it back to a list:
import json file = 'data.json'; data = json.loads(open(file).read()) print(data)
Print
[1, 2, 3, 4, 5, 6]
source share