Skip to content

Instantly share code, notes, and snippets.

@ireneisdoomed
Created February 1, 2023 14:13
Show Gist options
  • Save ireneisdoomed/43b09f83418cbe2e6cfc92e80b22d56d to your computer and use it in GitHub Desktop.
Save ireneisdoomed/43b09f83418cbe2e6cfc92e80b22d56d to your computer and use it in GitHub Desktop.
Pretty print JSON Schema
def json_extract(obj, key):
"""Recursively fetch values from nested JSON."""
arr = []
def extract(obj, arr, key):
"""Recursively search for values of key in JSON tree."""
if isinstance(obj, dict):
for k, v in obj.items():
if isinstance(v, (dict, list)):
extract(v, arr, key)
elif k == key:
arr.append(v)
elif isinstance(obj, list):
for item in obj:
extract(item, arr, key)
return arr
values = extract(obj, arr, key)
return values
json_ex = {
"fields": [
{
"metadata": {},
"name": "studyLocusId",
"nullable": False,
"type": "string"
},
{
"metadata": {},
"name": "variantId",
"nullable": False,
"type": "string"
},
{
"metadata": {},
"name": "chromosome",
"nullable": False,
"type": "string"
},
{
"metadata": {},
"name": "studyId",
"nullable": False,
"type": "string"
},
{
"metadata": {},
"name": "studyType",
"nullable": False,
"type": "string"
},
{
"metadata": {},
"name": "beta",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "oddsRatio",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "confidenceIntervalLower",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "confidenceIntervalUpper",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "pValueMantissa",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "pValueExponent",
"nullable": True,
"type": "long"
},
{
"metadata": {},
"name": "finemappingMethod",
"nullable": True,
"type": "string"
},
{
"metadata": {},
"name": "credibleSet",
"nullable": True,
"type": {
"containsNull": True,
"elementType": {
"fields": [
{
"metadata": {},
"name": "is95CredibleSet",
"nullable": True,
"type": "boolean"
},
{
"metadata": {},
"name": "is99CredibleSet",
"nullable": True,
"type": "boolean"
},
{
"metadata": {},
"name": "logABF",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "posteriorProbability",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "tagVariantId",
"nullable": True,
"type": "string"
},
{
"metadata": {},
"name": "tagPValue",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "tagPValueConditioned",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "tagBeta",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "tagStandardError",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "tagBetaConditioned",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "tagStandardErrorConditioned",
"nullable": True,
"type": "double"
},
{
"metadata": {},
"name": "r2Overall",
"nullable": True,
"type": "double"
}
],
"type": "struct"
},
"type": "array"
}
}
],
"type": "struct"
}
json_extract(json_ex["fields"][12], "name")
"""
['credibleSet',
'is95CredibleSet',
'is99CredibleSet',
'logABF',
'posteriorProbability',
'tagVariantId',
'tagPValue',
'tagPValueConditioned',
'tagBeta',
'tagStandardError',
'tagBetaConditioned',
'tagStandardErrorConditioned',
'r2Overall']
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment