Dump to JSON Adds Additional Double Quotes and Escaping of Quotes

Dump to JSON adds additional double quotes and escaping of quotes

You are double encoding your JSON strings. data is already a JSON string, and doesn't need to be encoded again:

>>> import json
>>> not_encoded = {"created_at":"Fri Aug 08 11:04:40 +0000 2014"}
>>> encoded_data = json.dumps(not_encoded)
>>> print encoded_data
{"created_at": "Fri Aug 08 11:04:40 +0000 2014"}
>>> double_encode = json.dumps(encoded_data)
>>> print double_encode
"{\"created_at\": \"Fri Aug 08 11:04:40 +0000 2014\"}"

Just write these directly to your file:

with open('data{}.txt'.format(self.timestamp), 'a') as f:
f.write(data + '\n')

How to correctly escape double quote () inside a json string in Python

Do you really need a string in the first place?

s = {"title": 'Fetching all Jobs from "host_name".'}

# If you want a string, then here
import json
j = json.dumps(s)
print(j)

The recycled value looks like so

{"title": "Fetching all Jobs from \"host_name\"."}
>>> s2 = r'{"title": "Fetching all Jobs from \"host_name\"."}'
>>> json.loads(s2)
{'title': 'Fetching all Jobs from "host_name".'}

Escape double quotes for JSON in Python

>>> s = 'my string with \\"double quotes\\" blablabla'
>>> s
'my string with \\"double quotes\\" blablabla'
>>> print s
my string with \"double quotes\" blablabla
>>>

When you just ask for 's' it escapes the \ for you, when you print it, you see the string a more 'raw' state. So now...

>>> s = """my string with "double quotes" blablabla"""
'my string with "double quotes" blablabla'
>>> print s.replace('"', '\\"')
my string with \"double quotes\" blablabla
>>>

Single vs double quotes in JSON

JSON syntax is not Python syntax. JSON requires double quotes for its strings.

Escape double quotes when converting a dict to json in Python

Your last attempt json.dumps({'foo': 'bar'}).replace('"', '\\"') is actually correct for what you think you want.

The reason you see this:

'{\\"foo\\": \\"bar\\"}'

Is because you're printing the representation of the string. The string itself will have only a single backslash for each quote. If you use print() on that result, you will see a single backslash

How to escape double quotes in JSON

Try this:

"maingame": {
"day1": {
"text1": "Tag 1",
"text2": "Heute startet unsere Rundreise \" Example text\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.</strong> "
}
}

(just one backslash (\) in front of quotes).

JSON Dump a string surrounded by single quotes and containing escaped single quotes

I want to give credit to my boy @Seth250 who gave me half of the solution

What I had to do is simply using the literal_eval but, instead of dumping it with the json lib, simply surrounding the literal_eval'ed string into a dict.

instance = ast.literal_eval(string_with_escapes)
instance_json = dict(instance)

the result will, infact, be:

{
'description': 'description',
'table_name': 'table_name',
'database_name': 'database_name'
}
which is exactly what I expected and now i can fully retrieve the fields i need.

json.dumps adding double quotes and backslash

It looks like your csv file is formatted in a way that confuses the default csv parser. In particular, your columns are separated by both a comma and a space:

           comma
v
"Test Name", "Status"
^
space

This makes the parser assume that all the text following the comma is part of the value, including the space and the quote mark.

One possible solution is to specify a value for the skipinitialspace argument when opening the DictReader.

import csv
import json

csvfile = open('fiile.csv','r')
jsonfile = open('file.json','w')

reader = csv.DictReader(csvfile, skipinitialspace=True)
jsonfile.write(json.dumps(list(reader)))

Then the parser should understand that the space and quote aren't part of the value. Result:

[{"Test Name": "Basic Tcp IPerf Test", "Status": "PASS", "Test Start Time": "20190103 08:07:41.662", "Test End Time": "20190103 08:08:44.051", "Throughput": "2095.2746", "Jitter": "", "Packet Loss %": "", "Compute/VM": "SendVm (x.x.x.x)", "Statistics Sampling Start Time": "03 Jan 2019 08:07:44", "Statistics Sampling End Time": "03 Jan 2019 08:08:42", "Min CPU": "0", "Max CPU": "2", "Avg CPU": "1", "Avg Memory (MB)": "7758"}]


Related Topics



Leave a reply



Submit