Last active
March 14, 2017 17:15
-
-
Save jakekara/6170372a471f009e39cb82d5e105d3e3 to your computer and use it in GitHub Desktop.
print the keys from any level of a json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# jsonkeys.py - print json keys | |
# | |
from __future__ import print_function | |
from sys import argv, stderr | |
import json | |
# | |
# usage - print usage and quite | |
# | |
def usage(): | |
print ("Usage: " + argv[0] + "filename:subkey_1:subkey_2:subkey_3", | |
file=stderr) | |
exit(1) | |
# | |
# whittle - Get a subobject by walking through keys array | |
# | |
def whittle(obj, keys): | |
for k in keys: | |
obj = obj[k] | |
return obj | |
# | |
# print_keys - print json keys given a string of the following format | |
# args: string formatted like "filename:subkey1:subkey2:subkey3" | |
# where subkey1 is at the top level of the json object, | |
# subkey2 is a child of subkey1, and subkey3 is a child of | |
# subkey3 | |
# note: there may be zero subkeys | |
# | |
def print_keys(cmd): | |
parts = cmd.split(":") | |
fname = parts[0] | |
subkeys = parts[1:] | |
try: | |
obj = json.loads(open(fname).read()) | |
except Exception, e: | |
print("Error loading JSON object from file: " + str(e), file=stderr) | |
exit(1) | |
obj = whittle(obj, subkeys) | |
try: | |
print (obj.keys()) | |
except: | |
print (type(obj)) | |
# | |
# main - parse args and call print_keys | |
# | |
def main(): | |
# test for input | |
if len(argv) < 2: | |
usage() | |
files = argv[1:] | |
# process files | |
for f in files: | |
print_keys(f) | |
main() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Script started on Tue Mar 14 13:11:42 2017 | |
bash-3.2$ ./jsonkeys.py tl_2016_09_tabblock10.json | |
[u'objects', u'type', u'transform', u'arcs'] | |
bash-3.2$ ./jsonkeys.py tl_2016_09_tabblock10.json:objects | |
[u'r9c3', u'r5c1', u'r0c2', u'r8c9', u'r8c8', u'r8c7', u'r8c6', u'r8c5', u'r8c4', u'r8c3', u'r8c2', u'r8c1', u'r3c0', u'r2c1', u'r3c2', u'r3c3', u'r3c4', u'r3c5', u'r3c6', u'r3c7', u'r3c8', u'r3c9', u'r2c3', u'r2c2', u'r2c5', u'r2c4', u'r2c7', u'r2c6', u'r1c3', u'r5c8', u'r5c9', u'r0c1', u'r5c2', u'r5c3', u'r0c0', u'r5c6', u'r5c7', u'r5c4', u'r5c5', u'r7c4', u'r7c5', u'r7c6', u'r4c8', u'r6c9', u'r6c8', u'r7c2', u'r7c3', u'r4c3', u'r4c2', u'r6c7', u'r6c2', u'r4c7', u'r4c6', u'r6c3', u'r7c1', u'r4c4', u'r7c7', u'r1c0', u'r4c0', u'r1c1', u'r6c5', u'r6c1', u'r4c9', u'r4c1', u'r6c6', u'r2c8', u'r7c8', u'r2c0', u'r7c9', u'r3c1', u'r1c2', u'r4c5', u'r9c8', u'r9c9', u'r9c6', u'r9c7', u'r9c4', u'r9c5', u'r9c2', u'r6c4', u'r9c1'] | |
bash-3.2$ ./jsonkeys.py tl_2016_09_tabblock10.json:type | |
<type 'unicode'> | |
bash-3.2$ ./jsonkeys.py tl_2016_09_tabblock10.json:transform | |
[u'translate', u'scale'] | |
bash-3.2$ ./jsonkeys.py tl_2016_09_tabblock10.json:transform:scale | |
<type 'list'> | |
bash-3.2$ exit | |
exit | |
Script done on Tue Mar 14 13:12:49 2017 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment