How to parse json text data with python - 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

Related

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 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"]}

Json object is printed in strange format in Python

so i do this get request to a Steam page where it responds this JSON:
{"success":true,"lowest_price":"$2.23","volume":"2,842","median_price":"$2.24"}
My objective is to transform it into a dictionary in Python, but what i get when i return the JSON object in my function is this:
{u'volume': u'2,842', u'median_price': u'2,02€ ',
u'lowest_price': u'1,99€ ', u'success': True} (notice the u').
What can i do to eliminate the u's?
You're seeing Python letting you know that the strings you're printing are unicode strings. Unless the output you're seeing really matters (e.g., it's input for something else), you can generally disregard the leading 'u' character until you run into issues with unicode output.
There are a litany of stack overflow questions which address this.
Python string prints as [u'String']
What's the u prefix in a python string
Printing a string prints 'u' before the string in Python?
And a lot more....
You could import json module and use json.dumps to prettify your output.
import json
response = {"success":True,"lowest_price":"$2.23","volume":"2,842","median_price":"$2.24"}
print json.dumps(response, indent=2)

Python simplejson quoting for R rjson input

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)

Python: Can I import slice of a json file with urllib?

I'm trying to import some JSON data into Python with urllib and json.load but I want to cut out the first x and the last y characters because it's making the json data invalid.
thePage = urllib.urlopen("http://datafile.dat")
myData = json.load(thePage)
I want to do something like json.load(thePage[10:-10]), but since urlopen doesn't return a string, I can't slice it. What can I do?
You can get the text from the request by doing .read(). With this, you should use json.loads() instead of json.load(), as you're inputting a string as a parameter.
You can do your normal slicing if necessary with the HTML as a string.

Categories

Resources