Python simplejson quoting for R rjson input - python

I'm processing data within Python, and would like to stream records to R using JSON formatting and simplejson on the Python side, and rjson on the R side.
How can I output records out of Python so that R's fromJSON can process them into a one-line dataframe? Thanks
try:
import simplejson as json
except ImportError:
import json
record = {'x':1,'y':1}
print json.dumps( record )
Result:
{"y": 1, "x": 1}
However, I'd need the result to be "{\"x\":1,\"y\":2}", as R needs that formatting to use the data:
library(rjson)
as.data.frame( fromJSON( "{\"x\":1,\"y\":2}" ) )
x y
1 1 2
Thanks.

Two options:
(1) If your JSON does not contain both single & double quotes, wrap the entire JSON in the quote type not being used.
(2) If you need to escape the quotes (ie, because both are in your JSON), then you need to escape the escape character. That is, use double slashes: \\"y\\"
(note that the second point applies to any string in R that has needs an escape character)

Related

Loads json with double quotes in it

I'm trying to parse a JSON string that contains double quotes in it:
import json
x = '''{"key":"Value \"123\" "}'''
When I try to load this JSON using the following statement
y = json.loads(x)
It raises the following exception:
Expecting ',' delimiter: line 1 column 15 (char 14)
As per my understanding, it is due to the double quotes around 123 in JSON. Also, I tried replacing the \" (backslash quote) with some other stuff as well but all in vain
x.replace('\"',"'")
as it also replaced the double quotes that are present around key and value as well
'''{"key": "Value \"123\" "}''' ---Replacing--> '''{'key':'Value '123' '}''')
I can not change anything in the input string. That's coming from an API.
Any help would be highly appreciated, I'm stuck with this for quite a time now. Thanks in advance...
\" within a string is simply a double quotation mark. You need to add another backslash:
x = '''{"key":"Value \\"123\\" "}'''

Converting backslash single quote \' to backslash double quote \" for JSON

I've got a JSON file that was converted to a string in Python. Somehow along the way the double quotes have gotten replaced with single quotes.
{\'MyJSON\': {\'Report\': \'1\' ....
I need to convert my string so that it is in this format instead:
{\"MyJSON\": {\"Report\": \"1\" ....
My problem is that using str.replace, I can't figure out how to convert a single quote into a double quote as both quotes are escaped.
My ultimate goal is to be able to put the string into json.loads so that I can pretty print it.
Attempts:
txt.replace(r"\'", r'\"')
> "{'MyJSON': {'Report': '1'"
txt.replace("\"", "\'")
> "{'MyJSON': {'Report': '1'"
If I save my string to a txt file it appears in the preview as:
{'MyJSON': {'Report': '1' ...
So I think what I actually need to do is replace ' with "
I have decided to use ast.literal_eval(txt) which can convert my string to a dictionary. From there, json.loads(json.dumps(dict)) gets me to JSON
i mean,
my_string = "\"\'"
print(my_string.replace("\'", "\""))
works perfectly fine
EDIT: i didn't mean use this directly, it was a proof of concept. In mine the replacement was reversed. I have updated this snippet such that it could directly be put into your code. Try it again
Instead of focusing on the backslashes to try to "hack" a json string / dict str into a JSON, a better solution is to take it one step at a time and start by converting my dict string into a dictionary.
import ast
txt = ast.literal_eval(txt) # convert my string to a dictionary
txt = json.loads(json.dumps(txt)) # convert my dict to JSON

How to parse json text data with python

I need to make a request to the api of my client, and the api returns this data:
[6,0,'VT3zrYA',5,'USUeZWA',5,0,0,0,0,0,4,0,0,0,2,0,0,3,0,0,0,0,2,0,1,["portale.titolari.client.config.ShoulderDTO/4121330600","java.util.HashSet/3273092938","MATTEO SBRAGIA","java.util.ArrayList/4159755760","java.util.Date/3385151746","MATTEO"],0,7]
How can I parse this data and extract the following fields :
MATTEO SBRAGIA
MATTEO
I've tried this code, but it's not working :
data = json.load(output_data)
pprint data
This in fact is not a valid JSON string because it contains single quotes '. You can replace all single quotes with double quotes and then parse the string but it's a question whether this was intentional or a mistake:
import json
s = '[6,0,\'VT3zrYA\',5,\'USUeZWA\',5,0,0,0,0,0,4,0,0,0,2,0,0,3,0,0,0,0,2,0,1,["portale.titolari.client.config.ShoulderDTO/4121330600","java.util.HashSet/3273092938","MATTEO SBRAGIA","java.util.ArrayList/4159755760","java.util.Date/3385151746","MATTEO"],0,7]'
data = json.loads(s.replace("\'", '"'))
print(data[26][2])
print(data[26][5])
prints:
$ python test.py
MATTEO SBRAGIA
MATTEO

How to parse data into proper json in python and remove slashes

I am getting this as my response
b'{"userdetails":[["{\\”user_id\\":[\\”54562af66ffd\\"],\\”user_name\\":[\\"bewwrking\\"],\\”room\\":[\\"31\\”]}'
I want to convert it into proper json without any double slashes.
Is there any buildin function for that or i need to do string replace
If you have control over how it is being sent, I would recommend doing to_string on any relevant field/keys that you are sending as json. I had some weird json responses before sanitizing the input to json_dump.
remove the leading b and run replace as below.
s = '{"userdetails":[["{\\"user_id\\":[\\"54562af66ffd\\"],\\"user_name\\":[\\"bewwrking\\"],\\"room\\":[\\"31\\"]}'
s = s.replace('\','')
print(s)
{"userdetails":[["{"user_id":["54562af66ffd"],"user_name":["bewwrking"],"room":["31"]}

Simplejson strange behavior

So In python I make a dictionary in JSON structure
>>> a = {"name":'nikhil',"age":25}
Now I check if a is a Valid JSON using http://jsonlint.com/
. I get it's valid.
Now I do :
>>> b = simplejson.dumps(a)
>>> b= '{"age": 25, "name": "nikhil"}'
Now I do:
>>> c = simplejson.loads(b)
>>> c = {'age': 25, 'name': 'nikhil'}
Now I check if c is a Valid JSON I get error.
Why is Simplejson is not able to convert JSON string back to valid JSON? when I started with a valid JSON only?
You are confusing JSON with Python here. b is a JSON-formatted string, c is a Python object.
Python syntax just happens to look a lot like JSON (JavaScript) in that respect.
Python strings can use either ' or ", depending on the contents; JSON always uses ". You entered a using double quotes for the keys, single quotes for the one string value; if you asked Python to echo it back for you you'll find it'll be shown with only single quotes.
Python booleans are True or False, JSON uses true and false.
The JSON 'empty' value is null, Python uses None instead.
See the Encoders and Decoders section of the json module for an overview how JSON and Python objects are mapped.

Categories

Resources