How to get rid of \n and in my json file

2024/11/15 17:29:33

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 but keeps them, so the file becomes faulty.

This is my code, it is python:

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()#print("Input text: ",text)#print(json.dumps(response, indent=2))json_data = (json.dumps(response, indent=2))json_arr.append (json_data)with open('data.json','w') as outline: #with open('data.json','w') as outline:json.dump(json_arr,outline)print("break")
print(json_arr)

['{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 113,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.734718,\n      "label": "/business and industrial/advertising and marketing/marketing"\n    },\n    {\n      "score": 0.675452,\n      "label": "/business and industrial/business operations/management/business process"\n    },\n    {\n      "score": 0.620496,\n    "label": "/business and industrial/advertising and marketing/brand management"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 215,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.913644,\n      "label": "/technology and computing/operating systems"\n    },\n    {\n      "score": 0.855434,\n      "label": "/technology and computing/hardware/computer"\n    },\n    {\n      "score": 0.842841,\n      "label": "/technology and computing/hardware/computer components/chips and processors"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 14,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.885687,\n      "label": "/automotive and vehicles/cars/hybrid"\n    },\n    {\n      "score": 0.821966,\n     "label": "/automotive and vehicles/electric vehicles"\n    },\n    {\n      "score": 0.77387,\n      "label": "/shopping/retail"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 86,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.967686,\n      "label": "/education/homework and study tips"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n  "text_characters": 80,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.822388,\n      "label": "/automotive and vehicles/cars/sedan"\n    },\n    {\n      "score": 0.754857,\n      "label": "/automotive and vehicles/cars/hybrid"\n    },\n    {\n      "score": 0.68194,\n      "label": "/automotive and vehicles/cars/car culture"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 13,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.921873,\n      "label": "/real estate/architects"\n    },\n    {\n      "score": 0.790383,\n      "label": "/business and industrial/construction"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 52,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    
{\n      "score": 0.939684,\n      "label": "/technology and computing/computer reviews"\n    },\n    {\n      "score": 0.931032,\n      "label": "/technology and computing/tech news"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 14,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.979729,\n      "label": "/education/homework and study tips"\n    
},\n    {\n      "score": 0.850809,\n      "label": "/real estate/architects"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 28,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.999484,\n      "label": "/education/teaching and classroom resources"\n    }\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 113,\n    "features": 
1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.734718,\n      "label": "/business and industrial/advertising and marketing/marketing"\n    },\n    {\n      "score": 0.675452,\n      "label": "/business and industrial/business operations/management/business process"\n    },\n    {\n      "score": 0.620496,\n      "label": "/business and industrial/advertising and marketing/brand management"\n    
}\n  ]\n}', '{\n  "usage": {\n    "text_units": 1,\n    "text_characters": 215,\n    "features": 1\n  },\n  "language": "en",\n  "categories": [\n    {\n      "score": 0.913644,\n      "label": "/technology and computing/operating systems"\n    },\n    {\n      "score": 0.855434,\n      "label": "/technology and computing/hardware/computer"\n    },\n    {\n      "score": 0.842841,\n      "label": "/technology and computing/hardware/computer components/chips and processors"\n    }\n  ]\n}']

Answer

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 herewith 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.

https://en.xdnf.cn/q/120430.html

Related Q&A

Python code to calculate the maximal amount of baggage is allowed using recursive function

I am new to python and I have an assignment, I need to write a recursive function that takes two arguments (Weights, W), weights is the list of weights of baggage and W is the maximal weight a student …

How to flatten a nested dictionary? [duplicate]

This question already has answers here:Flatten nested dictionaries, compressing keys(32 answers)Closed 10 years ago.Is there a native function to flatten a nested dictionary to an output dictionary whe…

Find an element in a list of tuples in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

print dictionary values which are inside a list in python

I am trying to print out just the dict values inside a list in python.car_object = {}cursor = self._db.execute(SELECT IDENT, MAKE, MODEL, DISPLACEMENT, POWER, LUXURY FROM CARS)for row in cursor:objectn…

Triangle of numbers on Python

Im asked to write a loop system that prints the following:0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 0 1 2 3 4 5 0 1 2 3 4 0 1 2 3 0 1 2 0 1 0However, my script prints this:0 1…

Using regex to ignore invalid syntax [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

What is the requirements.txt file? What should be in it in this particular case?

I am switching from Replit to PebbleHost to host my Python bot. What do I put in my requirements.txt file? These are the imports that I have at the start of my bot. import asyncio import datetime impo…

Is this a linux or a virtualenv error?

I asked this question 4 days ago.Now, when I open a terminal, I see this:and this:Is it related to the initial problem i had, or it a python and virtualenv issue?

Python code to authenticate to website, navigate through links and download files

Im looking something which could be interesting to you as well. Im developing a feature using Python, which should be able to authenticate (using userid/password and/or with other preferred authentica…

How to get sum of products of all combinations in an array in Python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…