Created
January 2, 2013 22:23
-
-
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)
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 | |
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