Created
February 14, 2023 14:29
-
-
Save crgimenes/4f0eb573a2a0e7a0c16dfb0e4171a7f2 to your computer and use it in GitHub Desktop.
Convert json to tuple (to be used in CSV generation for example)
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
import json | |
def json_titles_tuple(payload_json, prefix=''): | |
jsonobj = json.loads(payload_json) | |
titles = () | |
for key, value in jsonobj.items(): | |
if isinstance(value, dict): | |
titles += json_titles_tuple(json.dumps(value), prefix + key + '_') | |
else: | |
titles += (prefix + key,) | |
return titles | |
def json_values_tuple(payload_json): | |
jsonobj = json.loads(payload_json) | |
values = () | |
for _, value in jsonobj.items(): | |
if isinstance(value, dict): | |
values += json_values_tuple(json.dumps(value)) | |
else: | |
values += (value,) | |
return values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment