I have a json object that consists of one object with key 'data', that has values listed in a set of arrays. I need to return all arrays that contain the value x, but the arrays themselves do not have keys. I'm trying to write a script to enter a source file (inFile) an define an export file (outFile). Here is my data structure:
{ "data": [
["x", 1, 4, 6, 2, 7],
["y", 3, 2, 5, 8, 4],
["z", 5, 2, 5, 9, 9],
["x", 3, 7, 2, 6, 8]
]
}
And here is my current script:
import json
def jsonFilter( inFile, outFile ):
out = None;
with open( inFile, 'r') as jsonFile:
d = json.loads(json_data)
a = d['data']
b = [b for b in a if b != 'x' ]
del b
out = a
if out:
with open( outFile, 'w' ) as jsonFile:
jsonFile.write( json.dumps( out ) );
else:
print "Error creating new jsonFile!"
SOLUTION
Thanks to Rob and everyone for your help! Here's the final working command-line tool. This takes two arguments: inFile and Outfile. ~$ python jsonFilter.py inFile.json outFile.json
import json
def jsonFilter( inFile, outFile ):
# make a dictionary.
out = {};
with open( inFile, 'r') as jsonFile:
json_data = jsonFile.read()
d = json.loads(json_data)
# build the data you want to save to look like the original
# by taking the data in the d['data'] element filtering what you want
# elements where b[0] is 'x'
out['data'] = [b for b in d['data'] if b[0] == 'x' ]
if out:
with open( outFile, 'w' ) as jsonFile:
jsonFile.write( json.dumps( out ) );
else:
print "Error creating new JSON file!"
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('inFile', nargs=1, help="Choose the in file to use")
parser.add_argument('outFile', nargs=1, help="Choose the out file to use")
args = parser.parse_args()
jsonFilter( args.inFile[0] , args.outFile[0] );
outalwaysNone? Also, are you having any errors? what do you expect to happen?