Skip to content

Instantly share code, notes, and snippets.

@crgimenes
Created February 14, 2023 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crgimenes/4f0eb573a2a0e7a0c16dfb0e4171a7f2 to your computer and use it in GitHub Desktop.
Save crgimenes/4f0eb573a2a0e7a0c16dfb0e4171a7f2 to your computer and use it in GitHub Desktop.
Convert json to tuple (to be used in CSV generation for example)
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