Parsing JSON string/object in Python

2024/5/20 19:29:02

I've recently started working with JSON in python. Now I'm passing a JSON string to Python(Django) through a post request. Now I want to parse/iterate of that data. But I can't find a elegant way to parse this data, which somehow I'm pretty sure exists.

data = request.POST['postformdata']
print data
{"c1r1":"{\"Choice\":\"i1\"}","c2r1":"{\"Bool\":\"i2\"}","c1r2":"{\"Chars\":\"i3\"}"}jdata = json.loads(data)
print jdata
{u'c1r2': u'{"Chars":"i3"}', u'c1r1': u'{"Choice":"i1"}', u'c2r1': u'{"Bool":"i2"}'}

This is what was expected. But now when I want to get the values, I start running into problems. I have to do something like

mydecoder = json.JSONDecoder()
for part in mydecoder.decode(data):                                             print part
# c1r2 c1r1 c2r1 ,//Was expecting values as well

I was hoping to get the value + key, instead of just the key. Now, I have to use the keys to get values using something like

print jdata[key]

How do I iterate over this data in a simpler fashion, so that I can iterate over key, values?

Answer

To iterate key and value, you can write

for key, value in jdata.iteritems():print key, value

You can read the document here: dict.iteritems

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

Related Q&A

Removing NaNs in numpy arrays

I have two numpy arrays that contains NaNs:A = np.array([np.nan, 2, np.nan, 3, 4]) B = np.array([ 1 , 2, 3 , 4, np.nan])are there any smart way using numpy to remove the NaNs in b…

Run Python + OpenCV + dlib in Azure Functions

I have created an image processing script in Python (with dlib and OpenCV) - I was wondering how I can bring this functionality to Azure Functions, so that the script can be called via an API. As Pytho…

Best way to add python scripting into QT application?

I have a QT 4.6 application (C++ language) and i need to add python scripting to it on windows platform. Unfortunately, i never embed python before, and it seems to be a lot of different ways to do so.…

Unexpected behavior of python builtin str function

I am running into an issue with subtyping the str class because of the str.__call__ behavior I apparently do not understand. This is best illustrated by the simplified code below.class S(str):def __ini…

How to set cookies with GAE/Python for 1 month?

I need to implement the following:User input user id and pass We validate that on another server If they are correct, cookies with these details should be saved for one month Each time user uses my sit…

connection refused with Celery

I have a Django project on an Ubuntu EC2 node, which I have been using to set up an asynchronous using Celery. I am following How to list the queued items in celery? along with the docs, to experiment…

Routing all packets through my program?

I want to build an application that routes all network traffic (not just HTTP) through my application. Basically, what I want is all the traffic to be given to my application (they should never reach t…

python struct.pack(): pack multiple datas in a list or a tuple

Say i have a list or a tuple containing numbers of type long long,x = [12974658, 638364, 53637, 63738363]If want to struct.pack them individually, i have to use struct.pack(<Q, 12974658)or if i want…

Can I pass a list of colors for points to matplotlibs Axes.plot()?

Ive got a lot of points to plot and am noticing that plotting them individually in matplotlib takes much longer (more than 100 times longer, according to cProfile) than plotting them all at once. Howev…

Tidy data from multilevel Excel file via pandas

I want to produce tidy data from an Excel file which looks like this, with three levels of "merged" headers:Pandas reads the file just fine, with multilevel headers:# df = pandas.read_excel(t…