Skip to content Skip to sidebar Skip to footer

How To Get Rid Of "\n" And " ' ' " In My Json File

thanks for reading I am creating a json file as a result of an API that I am using. My issue is that the outcome gets has \h and '' in it and a .json file does not process the \n b

Solution 1:

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 herewithopen('data.json','w') as outline: #withopen('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.

Post a Comment for "How To Get Rid Of "\n" And " ' ' " In My Json File"