[Tutor] json file

Kent Johnson kent37 at tds.net
Sat Feb 13 16:11:18 CET 2010


On Sat, Feb 13, 2010 at 7:21 AM, Chris Patillo <pythonnewbie6 at gmail.com> wrote:
> I need to read in a .json file and write out a file with some of the
> information from this file.  I can't figure out how to read the .json file
> in.  I know there is a module for this, but I don't understand how it works.
>
> The file I am needing to read is like below (except much much larger)
>
> {
>
>    "table":"contacts",
>
>    "records":[
>
>       {
>
>          "name":"John",
>
>          "phone":"555-555-4444"
>
>       },
>
>       {
>
>          "name":"Jane",
>
>          "phone":"555-555-3333"
>
>       },
>
>    ]
>
> }
>
>
>
> I have no clue how to do this.  I was thinking something like;
>
> import json
>
> json.dumps([name,phone], seperators(":"))

json.dumps() converts a structure to a json string. json.load() is
what you want. If your file is called data.json, try
import json
data = json.load(open('data.json'))
print data

If your data is not Ascii or UTF-8 you have to specify the encoding to
json.load().

Kent


More information about the Tutor mailing list