Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

arrays - Parse json data in python

I am having trouble parsing the resulting json data to return only wanted section (e.g., 'name, 'aisle', 'status'). How can I modify the output to only print those items?

Code:

    for coro in tqdm(asyncio.as_completed(tasks, loop=loop)):
    try:
        response = await coro
        if response is None:
            continue
        data, store = response
        result = json.loads(data['searchResults'])['results'][0]
        summary = {
            'name': result['name'],
            'aisle': result['price']['aisle'][0],
            'status': result['inventory']['status'],
        }
        results[store] = summary
    except (IndexError, KeyError):
        continue

   with open('Testing.txt', 'w') as outfile:
   json.dump(results, outfile, indent = 2)
   outfile.write('
')

When I print I get the following format:

{
  "1": {
    "name": "camera",
    "aisle": "M.3",
    "status": "In Stock"
  },
   "2": {
    "name": "camera",
    "aisle": "M.53",
    "status": "Out of Stock"  
  },
   "3":{
    "name": "camera",
    "aisle": "M.32",
    "status": "In Stock"
  }
}

I would like the output for each loop on a single line, such as:

    '35': { 'name': 'Camera', 'aisle': 'M.35', 'status': 'Out of stock' },
    '36': { 'name': 'Camera', 'aisle': 'J.35', 'status': 'In stock' }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

FYI—the sample data from the output file looks wrong because the value string is not valid json. It should be like this:

"{"results":[{"name":"Camera","department":{"name":"Electronics","storeDeptId":-1},"location":{"aisle":["M.35"],"detailed":[{"zone":"M","aisle":"36","section":"2"}]},"price":{"priceInCents":49900,"isRealTime":true,"currencyUnit":"USD"},"inventory":{"quantity":3,"status":"Out of stock","isRealTime":true}}]}"

Note the ] that is in my version of your JSON but not in yours. Once you get to valid JSON, you can use json.loads to transform that JSON string into a value that you can pull data out of:

data = json.loads(data['searchResults'])
print json.dumps(data, indent=2)

which should get you:

{
  "results": [
    {
      "department": {
        "name": "Electronics",
        "storeDeptId": -1
      },
      "inventory": {
        "status": "Out of stock",
        "isRealTime": true,
        "quantity": 3
      },
      "price": {
        "priceInCents": 49900,
        "isRealTime": true,
        "currencyUnit": "USD"
      },
      "name": "Camera",
      "location": {
        "detailed": [
          {
            "aisle": "36",
            "section": "2",
            "zone": "M"
          }
        ],
        "aisle": [
          "M.35"
        ]
      }
    }
  ]
}

Now, something like this will get you close to the trimmed output you want:

for coro in asyncio.as_completed(tasks, loop=loop):
    try:
        data, store = await coro
        result = json.loads(data['searchResults'])['results'][0] #Storing retrieved json data
        summary = {
            'name': result['name'],
            'aisle': result['location']['aisle'][0],
            'status': result['inventory']['status'],
        }
        results[store] = summary
    except (IndexError):
        continue

After this, the output in your output file will look something like:

'35': { 'name': 'Camera', 'aisle': 'M.35', 'status': 'Out of stock' },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...