I am getting some data from an API, that I want to then import into a BigQuery table. The doc specifies :
JSON data must be newline delimited. Each JSON object must be on a separate line in the file.
And I cannot seem to find a way to convert my JSON.
Here is my code, getting the first 2 lines of the API response and printing them :
cols = ["id","email","surname"]
for line in response.text.split("\n")[1:3]:
obj={}
for name,value in zip(cols,line.split(";")):
obj[name] = value
data.append(obj)
print(json.dumps(data))
The output is the following :
[{"id": "1", "email": "[email protected]", "surname": "Alice"}, {"id": "2", "email": "[email protected]", "surname": "Bob"}]
How should I proceed to format the JSON like this ?
{"id": "1", "email": "[email protected]", "surname": "Alice"}
{"id": "2", "email": "[email protected]", "surname": "Bob"}
Thanks in advance
CodePudding user response:
Writing an answer since comments are limited regarding formatting. Like you wrote, you should handle each dictionary as a separated json:
output = ''
cols = ["id","email","surname"]
for line in response.text.split("\n")[1:3]:
obj={}
for name,value in zip(cols,line.split(";")):
obj[name] = value
output = json.dumps(obj) '\n'
print(output)
CodePudding user response:
Another way of doing it is the following :
cols = ["id","email","surname"]
for line in response.text.split("\n")[1:3]:
obj={}
for name,value in zip(cols,line.split(";")):
obj[name] = value
data.append(obj)
myString = "\n".join([json.dumps(row) for row in data])