Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[{'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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading