Skip to content

Instantly share code, notes, and snippets.

@rpetrenko
Created June 9, 2016 20:48
Show Gist options
  • Save rpetrenko/f77e6287f7c663899d59ce610a6f1ba9 to your computer and use it in GitHub Desktop.
Save rpetrenko/f77e6287f7c663899d59ce610a6f1ba9 to your computer and use it in GitHub Desktop.
convert json to a list of key/values
#!/usr/bin/env python
"""
convert json to a flattened list of key/values
example: data = {
"a": [
{
"key1": 1
},
{
"key2": 2
}
]
}
will be converted to a list of key/value pairs:
[
[data["a"][0]["key1"], 1],
[data["a"][1]["key2"], 2]
]
author: rpetrenko
"""
import json
from pprint import pprint
import argparse
def read_file(fname):
print "Original json:"
with open(fname) as json_data:
data = json.load(json_data)
json_data.close()
pprint(data)
return data
OUTPUT = []
# disable if no need in empty lists
SKIP_EMPTY_DATA = not True
def _x(s, d):
if type(d) in [dict, list] and not SKIP_EMPTY_DATA and len(d) == 0:
OUTPUT.append([s, d])
if type(d) == dict:
for k, v in d.items():
_x("%s[\"%s\"]" % (s, k), v)
elif type(d) == list:
for i in range(len(d)):
_x("%s[%s]" % (s, i), d[i])
else:
OUTPUT.append([s, d])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('filename', help='file name with json data')
args = parser.parse_args()
out_js = read_file(args.filename)
_x("out_js", out_js)
print "Flattened json as a list of key/values:"
for line in OUTPUT:
k, v = line
if eval(k) == v:
print k, '=', v
else:
print "[%s][%s]" % (type(eval(k)), type(v))
print "[%s][%s]" % (eval(k), v)
assert eval(k) == v
print "list size", len(OUTPUT)
@vfursov
Copy link

vfursov commented Jun 9, 2016

cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment