Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Can someone please recommend me a tutorial on python json library? I am trying to understand how it works.

The tutorial on the Python website is not enough/too short.

share|improve this question
18  
There are only two relevant operations Python-to-JSON (dump) and JSON-to-Python (load). What is your question? How does the tutorial fail? What do you want to do that the tutorial doesn't explain? Can you be more specific on your problem? How does the tutorial fail to help? –  S.Lott Jan 21 '11 at 14:22
 
The tutorial shows someone typing away at the command line. It doesn't show real clear code examples. If you get a different result from those few snippets in your code, it is no help. –  Ed Randall Dec 1 '13 at 16:32
add comment

3 Answers

up vote 144 down vote accepted

Try this: Python Module of the Week: json. One interesting point the author makes is to compare the behavior to pickling. It's not a clean analogy but it helps to get your head around it at first.

share|improve this answer
2  
That is indeed a helpful post. One thing I've found that speeds thing along for me when working with other data as well is using jsonlint: jsonlint.com The json.load() call has the least helpful error checking. In python2.7 at least. I found the linter above immensely helpful. –  mcpeterson Mar 8 '13 at 21:52
2  
The Python Module of the Week link above is a good start, but it is missing a simple json encode/decode example that works with something like a django web server. To encode your data: r=json.dumps( [ { 'field':'value1', 'anotherField':'value' } ] ) Then to decode the data: resp = json.loads( response.content ). Then resp[0]['field'] will give you 'value1'. The [0] is needed because json.loads() gives you a single element list containing a dictionary which has your json data in it –  TD Scott Mar 31 '13 at 15:45
 
Assuming you know what pickling is. I didn't :) –  ffledgling Oct 29 '13 at 10:24
 
ah, sorry about that. after like 8 years of python I take some things for granted. Pickling is just object serialization. –  jaydel Nov 3 '13 at 16:23
add comment

I guess this is a common misconception for people who want to work with json hierarchies using python.

json, once loaded into python is nothing more than a (perhaps complex) dictionary. A dictionary is a standard data structure in python. You will have to understand this type of structure if you want to work with data structures that were json before you loaded them into python.

So rather than searching for how to work with json in python (which is very easy - mainly just load and dump), learn how to work with complicated dicts that contain lists with dicts.

share|improve this answer
 
Great! This is enlighten me. I just wondering that it closely match python dict data structure. And you emphasize it correctly –  swdev Sep 28 '13 at 5:35
add comment

python standard library can use json file edit. You can try http://docs.python.org/2/library/json.html That will be a good enough to handle json .

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.