Last active
May 4, 2020 08:33
-
-
Save void285/af9ad2824b1a30e1645a999c6f8c9f8d to your computer and use it in GitHub Desktop.
This script helps you explore json file structure by remove [1:] elements from every array recursively. It's usefull when you have a big event giant json file and want to have a glance at it structure. It's in python2 style.
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
# coding: utf-8 | |
import json | |
import codecs | |
import sys | |
def parse_json(data): | |
global depth | |
depth += 1 | |
if type(data) == list: | |
if len(data) > 0 and type(data[0]) in (dict, list): | |
data = [parse_json(data[0])] | |
depth -= 1 | |
print("\t" * (depth - 1) + str(depth)) | |
# if has 2 plain values, maybe a "list style dict" | |
if len(data) > 2: | |
data = [data[0]] | |
return data | |
elif type(data) == dict: | |
data2 = {} | |
for k, v in data.iteritems(): | |
if type(v) in (dict, list): | |
v = parse_json(v) | |
depth -= 1 | |
print("\t" * (depth - 1) + str(depth)) | |
data2[k] = v | |
return data2 | |
print("\t" * (depth - 1) + str(depth)) | |
return data | |
depth = 0 | |
data = json.loads(codecs.open(sys.argv[1], "r", "utf-8").read()) | |
codecs.open(sys.argv[1] + "_sturcture.json", "w", "utf-8").write(json.dumps(parse_json(data))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment