I have a JSON file that includes many documents. Each document is data from one Purchase Order. I get file from a web service on a cloud purchase order system. I need to load each of these documents into a separate record in an Oracle database. I have done this for other files of JSON documents using Oracle's external table feature, and it has worked. However, the other files had a crlf between each JSON document. The file I get from the web service is one document with many POs, with no crlf between the purchase orders.
I found the Q&A here: How to split json into multiple files per document. The code shown as the solution is
import json
in_file_path='path/to/file.json' # Change me!
with open(in_file_path,'r') as in_json_file:
# Read the file and convert it to a dictionary
json_obj_list = json.load(in_json_file)
for json_obj in json_obj_list:
filename=json_obj['_id']+'.json'
with open(filename, 'w') as out_json_file:
# Save each obj to their respective filepath
# with pretty formatting thanks to `indent=4`
json.dump(json_obj, out_json_file, indent=4)
but when I try the solution, I get an error as below:
[oracle@localhost gk]$ python36 split.py
Traceback (most recent call last):
File "split.py", line 11, in <module>
filename=json_obj['_id']+'.json'
TypeError: string indices must be integers
My JSON file looks like:
{
"data": [
{
"number": "PB510698",
"uuid": "9cc06f21c1194038b137cec51b02606b"
},
etc ...
]
}
with multiple docs (sub docs?) that start with {"number":"PB510698","uuid"
Any ideas why the code from the other post is not working?