create array of object with new line ( newline Json standard format conversion for BigQuery)

I’ve created a json object to store it in Cloud Storage but I need it to be converted as newline Json standard format so BigQuery can read it.

This is my code:

  items = []
  for item in item_list:
    item = {'key': item}
    items.append(item)

The actual current output looks like this:

[{'item': 'stuff'}, {'item': 'stuff'}, {'item': 'stuff'}, {'item': 'stuff'}]

And I need it to be like this:

{'item': 'stuff'}
{'item': 'stuff'}
{'item': 'stuff'}
{'item': 'stuff'}

From what I understand I need to add new line '\n' between each object on my array. How can I do this?

>Solution :

So first we cast the list to a String. Then we replace the "[" with a "[\n", so it has a newline after the square brackets. Then we replace "]" with a "\n]" for the same reason. Lastly we replace all the "}, " with "}, \n"

jsonString = str([{'item': 'stuff'}, {'item': 'stuff'}, {'item': 'stuff'}, {'item': 'stuff'}])

jsonString = jsonString.replace("[", "[\n")
jsonString = jsonString.replace("]", "\n]")
jsonString = jsonString.replace("}, ", "},\n")
print(jsonString)

Leave a Reply