Skip to content

Instantly share code, notes, and snippets.

@mediocregopher
Created January 2, 2013 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mediocregopher/4438838 to your computer and use it in GitHub Desktop.
Save mediocregopher/4438838 to your computer and use it in GitHub Desktop.
Reads in lines, interprets them as json, then recursively replaces all dictionary values with '...' and all lists with [ '...' ]. The idea is that you can use this to get the "gist" of the json struct, so if you have a lot of json structs you're analyzing to find similarities this may help. (Works on py2 and py3)
#!/usr/bin/env python
import sys
import json
def main():
for line in sys.stdin:
struct = json.loads(line)
clean_struct(struct)
clean_line = json.dumps(struct,sort_keys=True)
print(clean_line)
def clean_struct(struct):
for key in struct:
val = struct[key]
if isinstance(val,dict):
struct[key] = clean_struct(val)
elif isinstance(val,list):
struct[key] = [ '...' ]
else:
struct[key] = '...'
return struct
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment