The fundamental issue is that you’re creating multiple JSON strings (json_data = (json.dumps(response, indent=2))
) and then adding them to an array (json_arr.append(json_data)
), and then converting that array to JSON (json.dump(json_arr,outline)
). So the result is a JSON array of strings (containing JSON).
You don’t want to create a string before adding to the array, just include response
in json_arr
. Then it all gets converted together later by your existing json.dump(json_arr,outline)
call:
json_arr = []
for text in gtm_Q6_df['ANSWER']:
response = natural_language_understanding.analyze(
text=text,language="en",
features=Features(
categories=CategoriesOptions(limit=3),
)).get_result()
json_arr.append (response) # <==== change is here
with open('data.json','w') as outline: #with open('data.json','w') as outline:
json.dump(json_arr,outline)
print("break")
print(json_arr)
You may need to tweak that slightly, I don’t do much Python, but the fundamental issue is that you’re converting the data in each reponse
to JSON twice. Do it just once, all together, at the end.
1
solved How to get rid of “\n” and ” ‘ ‘ ” in my json file