-2

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('\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' }
Mia Ella
  • 37
  • 5
  • Have you read the [`json`](https://docs.python.org/3/library/json.html) docs by any chance? They explain how to convert JSON data into Python dicts, then you simply need to access the appropriate dict elements and print them however you like. – MattDMo Jan 08 '17 at 19:57
  • In that case you better to off using json and just joining the items and write them to a text file. – Mazdak Jan 08 '17 at 19:57
  • Thanks - I updated my post above but am getting the following error: NameError: name 'data' is not defined – Mia Ella Jan 08 '17 at 21:36

1 Answers1

1

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' },
2ps
  • 15,099
  • 2
  • 27
  • 47
  • Thanks - I updated my post above but am getting the following error: NameError: name 'data' is not defined – Mia Ella Jan 08 '17 at 21:36
  • The last two lines of your updated code block are not needed. Replace them with your original code for outputting the txt file. – 2ps Jan 08 '17 at 21:41
  • How could I modify the 'write to file' so that the results of each loop are on individual lines? Right now I am getting a new line for each data point. – Mia Ella Jan 08 '17 at 22:29
  • http://stackoverflow.com/questions/16264515/json-dumps-custom-formatting – 2ps Jan 09 '17 at 04:52