Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a json string to python object

Tags:

python

json

Is it possible to convert a json string (for e.g. the one returned from the twitter search json service) to simple string objects. Here is a small representation of data returned from the json service:

{ results:[...], "max_id":1346534, "since_id":0, "refresh_url":"?since_id=26202877001&q=twitter", . . . } 

Lets say that I somehow store the result in some variable, say, obj. I am looking to get appropriate values like as follows:

print obj.max_id print obj.since_id 

I've tried using simplejson.load() and json.load() but it gave me an error saying 'str' object has no attribute 'read'

like image 875
deostroll Avatar asked Oct 02 '10 20:10

deostroll


People also ask

What converts .JSON file into a Python object?

Use jsonpickle module to convert JSON data into a custom Python Object. jsonpickle is a Python library designed to work with complex Python Objects. You can use jsonpickle for serialization and deserialization complex Python and JSON Data.

How do you deserialize a JSON string in Python?

To deserialize the string to a class object, you need to write a custom method to construct the object. You can add a static method to ImageLabelCollection inside of which you construct Label objects from the loaded JSON dictionary and then assign them as a list to the class variable bbox.

How do you read a JSON string in Python?

To convert string to json in Python, use the json. loads() function. The json. loads() is a built-in Python function that accepts a valid json string and returns a dictionary to access all elements.

How do I convert a JSON to a string?

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.


2 Answers

I've tried using simplejson.load() and json.load() but it gave me an error saying 'str' object has no attribute 'read'

To load from a string, use json.loads() (note the 's').

More efficiently, skip the step of reading the response into a string, and just pass the response to json.load().

like image 199
Ben James Avatar answered Sep 28 '22 11:09

Ben James


if you don't know if the data will be a file or a string.... use

import StringIO as io youMagicData={ results:[...], "max_id":1346534, "since_id":0, "refresh_url":"?since_id=26202877001&q=twitter", . . . }  magicJsonData=json.loads(io.StringIO(str(youMagicData)))#this is where you need to fix print magicJsonData #viewing fron the center out... #youMagicData{}>str()>fileObject>json.loads #json.loads(io.StringIO(str(youMagicData))) works really fast in my program and it would work here so stop wasting both our reputation here and stop down voting because you have to read this twice  

from https://docs.python.org/3/library/io.html#text-i-o

json.loads from the python built-in libraries, json.loads requires a file object and does not check what it's passed so it still calls the read function on what you passed because the file object only gives up data when you call read(). So because the built-in string class does not have the read function we need a wrapper. So the StringIO.StringIO function in short, subclasses the string class and the file class and meshing the inner workings hears my low detail rebuild https://gist.github.com/fenderrex/843d25ff5b0970d7e90e6c1d7e4a06b1 so at the end of all that its like writing a ram file and jsoning it out in one line....

like image 38
Rex Fender Baird Avatar answered Sep 28 '22 12:09

Rex Fender Baird