简体   繁体   中英

Format JSON objects in python

I have json objects in a notepad(C:\\data.txt).There are millions of records I just used one record as an example.But I want to see only data on my notepad like: 1 123-567-9876 TEST1 TEST 717-567-9876 Harrisburg null US_PA

I dont want paranthesis,etc Once I get the clean data,plan is to import the data from notepad(say C:\\data2.txt) into SQL database.

This is the format of json object.

{
    "status":"ok",
      "items":[
  {
     "1":{

        "Work_Phone":"123-567-9876",
        "Name_Part":[
           "TEST1",
           "TEST"
        ],
        "Residence_Phone":"717-567-9876",
        "Mailing_City":"Harrisburg",
        "Mailing_Street_Address_line_1":"",
        "Cell_Phone":null,
        "Mailing_Country_AND_Province_OR_State":"US_PA"

     }
      }
   ]
}

Can someone pls help with python code to format this json object and export it to notepad.

You can use

import simplejson as json

Then you can open your file and load it into a python-Dictionary:

f = file("C:/data.txt","r")
data = json.loads(f.read())

But this works only, when the json-objects are stored in an array in your file. So this has to look like this:

[{ ... first date ...},
{... second date ...},
...,
{... last date ...}]

Then in data there is an array of dictionaries. Now you can write the dates in another file:

g = file("output.txt","w")
for d in data:
    for i in items:
        for k in i.keys:
             g.write(... some string build from the parameters ...)

If well done the file output.txt contains the lines. In detail it might be difficult becaus each item seems to contain some arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM