Populating key-value pair data to a data.frame

Some Dewey data includes JSON type key-value pair data. For example, Advan monthly patterns have field POPULARITY_BY_DAY and the data looks like


These are JSON texts.

deweydatar library has a function keyval_to_df to convert those key-value pair data to a data.frame.

To install deweydatar (version 0.1.2 or later),

# Load "devtools" library (install it first, if don't have it)
# Install deweydatar package from GitHub

and see here for more details.

Once you have the library,

apikey_ = "Your API key"
# Advan product path
pp_advan_mp = "Your API endpoint for Advan monthly patterns"

data = read_sample_data0(apikey_, pp_advan_mp, 100)

I will choose the first 6 rows of POPULARITY_BY_DAY as an example.

keyval_data = data$POPULARITY_BY_DAY[1:6]

This column has Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday as key values always. When the JSON format is regular, meaning each row has same keys in the same order, and there is no error in the data column, then you can call keval_to_df with asis=TRUE

# When fixed format with no error
df = keyval_to_df(keyval_data, asis = T)

You can select only some keys out of full keys.

# Select keys
df = keyval_to_df(keyval_data, keys = c("Saturday", "Tuesday"))

This example column is regular but in some other data fields, keys are irregular in each row (one example is provided later). For example, row 1 may have Saturday and Tuesday while row 2 has Saturday, Tuesday and Holiday. When you choose non-existing keys, the function will fill that key with NAs.

# Select not existing keys
df = keyval_to_df(keyval_data, keys = c("Saturday", "Holiday", "Tuesday"))

There are cases when certain rows have non-JSON text or error such as blank text (""). I intentionally made keyval_data[3] to "". If you run this ```asis = TRUE``, it will throw an error because the function does not know what to do with the data.

# When data has error JSON
keyval_data[3] = ""
df = keyval_to_df(keyval_data, asis = T)

You can provide specific keys to way around this error. Below code will fill the 3rd row with all NAs to populate a data.frame.

df = keyval_to_df(keyval_data, keys = c("Saturday", "Holiday", "Tuesday"))

Some columns have irregular keys. For example, RELATED_SAME_MONTH_BRAND has different brand names as keys in each row. Here is an example.

{"Starbucks":540,"Western Union":538,"Keyme Kiosk":507,"CVS":391,"CVS Pharmacy":385,"Rotary Club":356,"Wetzel's Pretzels":355,"Shoe Palace":350,"Shell Oil":349,"Chevron":346,"Vans":333,"Farmers Insurance Group":331,"76":320,"Cambria Suites":317,"Charley's":312,"Zales":303,"Sunglass Hut":301,"Jersey Mike's":298,"LIDS":297}
{"Legacy Health":47,"Western Union":40,"Starbucks":38,"Fred Meyer":25,"Chevron":24,"Kroger Pharmacy":24,"76":23,"Keyme Kiosk":21,"Fred Meyer Jewelers":21,"Jamba":20,"Subway":19,"Costco":19,"Hertz":19,"Costco Optical":18,"Fred Meyer Pharmacy":18,"Costco Pharmacy":18,"McDonald's":18,"U.S. Bank":17,"CVS Pharmacy":17}
{"Hanger Prosthetics & Orthotics":488,"Western Union":462,"Starbucks":379,"Kroger Pharmacy":316,"Walmart":315,"Shell Oil":311,"Fry's Food & Drug Stores":310,"Keyme Kiosk":304,"Walmart Vision Center":293,"Walmart Pharmacy":292,"Jackson Hewitt Tax Service":284,"Walmart Photo Center":283,"Circle K":272,"CVS":240,"Walgreens Pharmacy":230,"Walgreens":221,"Chevron":211,"CVS Pharmacy":211,"Panda Express":207}

If you want to extract specific brands, you can call

# Extract specific keys
keyval_data = data$RELATED_SAME_MONTH_BRAND[1:6]
df = keyval_to_df(keyval_data,
                   keys = c("Starbucks", "Walmart", "Target", "McDonald's"))

Finally, some Dewey data has key-value pair but not in JSON format. Here is an example for pass_by data.

pp_passby_visitors = "Your API end point"
data = read_sample_data0(apikey_, pp_passby_visitors, 100)

VISITOR_INDUSTRY column has key-value pair data but not in JSON

keyval_data = data$VISITOR_INDUSTRY[1:6]

This can be populated as well by setting type="pass_by"

df = keyval_to_df(keyval_data, type = "pass_by", asis = T)

This can be slow for a large dataset.
Please report if you find any errors.


1 Like