JSON print out dictonary

I’m trying to print out information from a JSON but having some troubles.

The format is like this:

{
  "Thailand": [
    {
      "date": "2020-1-22",
      "confirmed": 2,
      "deaths": 0,
      "recovered": 0
    },
    {
      "date": "2020-1-23",
      "confirmed": 3,
      "deaths": 0,
      "recovered": 0
    },
    ...
  ],
  ...
}

What I would like to do is print the country name in one column and then print the info from each dictionaries keys in the adjacent column, like below:

Canada | 0,0,0 | 1,0,0 |5,0,0

Columns would append based on a new date being added. So far I have no problem loading the JSON into TD’s web DAT, then printing out the bulk of the data into a table. However, I had to make another table with the list of countries and reference that. I also cannot access the keys separately, meaning I’m just printing out all the data for a country into 1 row.

import json
response = op('web1').text
importData = json.loads(response)

def onValueChange(channel, sampleIndex, val, prev):
#loads table
	for i in range(int(op('numCountries')['numCountries'])):
		country = str(op('countryList')[i,0])
	return

Any ideas?

WHOOP! Figured it out how to get the key values.
Here is what I did:

op(‘table1’)[0,0] = importData[‘country’][1][‘date’]

Hey @philrock

Nice work! just a note, it looks like you are using the now deprecated web DAT. You might like testing the new Web Client DAT which with its’ callbacks might make it a cleaner process to parse the json data.

Generally when going through json dictionaries such as these you can also:

import json
def onResponse(webClientDAT, statusCode, headerDict, data):
	countryDAT = op('country')	

	# convert json to dict
	response = json.loads(data)
	
	# get all countries by looping through keys
	for c in response.keys():
	
		# make a list with the country being the first entry
		myDataRow = [c]
		
		# get the last entry in this countries data list
		countryDict = response[c][-1]
		
		# loop over all keys in that last entry
		for cInfo in countryDict.keys():
			myDataRow.append(countryDict[cInfo])
		
		# append this list to a table
		countryDAT.appendRow(myDataRow)
	return

You probably would want to add something in where you check the table if the country is already in the list and if so, just replace the row. You can find all the available Methods for DATs here:

https://docs.derivative.ca/TableDAT_Class#Modifying_Table_Content

cheers
Markus