'Convert json data to newline delimited json to support BQ load using python

I am creating a josn file in python cloud functions which will load into BQ in later stage.

Input json:

{"data": [{"DATE": "2005-10-14 00:00:00", "ID": 21.0, "NAME": "NOM"}, {"DATE": "2005-10-14 00:00:00", "ID": 22.0, "NAME": "WW"}]}

Expected json with newline delimiter :

{"DATE": "2005-10-14 00:00:00", "ID": 21.0, "NAME": "NOM"}
{"DATE": "2005-10-14 00:00:00", "ID": 22.0, "NAME": "WW"}

Here is sample code to achieve this

dat = jsondata['data']
result = [json.dumps(record) for record in dat]
for i in result:
    print(i+'\n')

(i+'\n') :=> correctly capturing each record but please help me to capture all the records into a string variable (by appending or update or anyother) because I will again apply json.dumps(json_content) to pass to another function for processing.



Solution 1:[1]

I have declared a string variable to handle my situation.

json_content = ""
dat = jsondata['data']
result = [json.dumps(record) for record in dat]
for i in result:
    json_content+=str(i+'\n')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 raj S