Skip to content

Instantly share code, notes, and snippets.

@rg3915
Created February 14, 2023 05:48
Show Gist options
  • Save rg3915/5d988e30f161c2bbf014edceffe39a0d to your computer and use it in GitHub Desktop.
Save rg3915/5d988e30f161c2bbf014edceffe39a0d to your computer and use it in GitHub Desktop.
Extract all keys recursively - all keys json
import json
data = """
{
"data": {
"items": [
{
"name": "SciELO Preprints",
"type": "dataverse",
"url": "https://data.scielo.org/dataverse/preprints",
"identifier": "preprints",
"description": "",
"published_at": "2020-09-11T16:26:43Z"
},
{
"name": "An Academic Publishers’ GO FAIR Implementation Network (APIN)",
"type": "dataset",
"url": "https://doi.org/10.48331/scielodata.9CT6WT",
"global_id": "doi:10.48331/scielodata.9CT6WT",
"description": "",
"published_at": "2021-01-20T18:35:39Z",
"publisher": "SciELO Data",
"citationHtml": "",
"identifier_of_dataverse": "scielodata",
"name_of_dataverse": "SciELO Data",
"citation": "",
"storageIdentifier": "s3://10.48331/scielodata.9CT6WT",
"subjects": [
"Computer and Information Science"
],
"fileCount": 2,
"versionId": 11,
"versionState": "RELEASED",
"majorVersion": 1,
"minorVersion": 0,
"createdAt": "2021-01-20T18:14:19Z",
"updatedAt": "2021-01-20T18:35:39Z",
"contacts": [
{
"name": "Blog SciELO em Perspectiva",
"affiliation": ""
}
],
"publications": [
{
"url": "https://content.iospress.com/articles/information-services-and-use/isu200102"
}
],
"authors": [
"Jan Velterop",
"Erik Schultes"
]
}
]
}
}
"""
json_data = json.loads(data)
def get_all_keys(data):
# define a function to extract all keys recursively
keys = set()
if isinstance(data, dict):
for key, value in data.items():
keys.add(key)
keys.update(get_all_keys(value))
elif isinstance(data, list):
for item in data:
keys.update(get_all_keys(item))
return keys
# call the function to get all keys
all_keys = get_all_keys(json_data)
# print the keys
print(all_keys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment