简体   繁体   中英

Parse json data in python

I am having trouble parsing the resulting json data to return only wanted section (eg, '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('\n')

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' }

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' },

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