0

I was following the instructions mentioned in the following question to convert JSON data to a dataframe using RJSONIO package. Link below:

How to convert JSON to Dataframe

Below is the JSON summary of my data, each field contains equal number of values, somewhere around 50,000. The value in color field is of type list, my guess is that is what is causing the problem.

json
title: chr
remaining: chr
color: list()
brand: chr
modelnum: chr
size: chr

I am attaching a sample set of JSON values, if anyone on the community can shed some light on how to model this into a dataframe, it'll be great!

Sample JSON data:

{"title":"oneplus 3","remaining":"","color":[],"brand":"OnePlus","modelnum":"OnePlus 3","size":""}
{"title":"oneplus 3 (soft gold, 64 gb)","remaining":"(soft )","color":["gold"],"brand":"OnePlus","modelnum":"OnePlus 3","size":"64 gb"}
{"title":"deal 1:oneplus 3 (graphite, 64gb) 6gb ram 4g lte - 1 year manufacture warranty","remaining":"deal 1:  6gb ram 4g lte - 1 year manufacture warranty","color":["graphite"],"brand":"OnePlus","modelnum":"OnePlus 3","size":"64gb"}
{"title":"oneplus 3 (graphite, 64 gb)","remaining":"","color":["graphite"],"brand":"OnePlus","modelnum":"OnePlus 3","size":"64 gb"}
{"title":"xiaomi redmi note 3 32gb","remaining":"","color":[],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"}
{"title":"xiaomi redmi note 3 (grey 32 gb) mobile phone","remaining":"mobile phone","color":["grey"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32 gb"}
{"title":"xiaomi redmi note 3 new (6 month brand warranty)","remaining":"new (6 month brand warranty)","color":[],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":""}
{"title":"xiaomi redmi note 3 (gold 32gb) mobile phone","remaining":"mobile phone","color":["gold"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"}
{"title":"xiaomi redmi note 3 (dark grey) (32gb)","remaining":"","color":["dark grey"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"}
{"title":"mi redmi note 3 32gb dark grey","remaining":"mi","color":["dark grey"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"}
{"title":"xiaomi redmi note 3 (gold, 32gb)","remaining":"","color":["gold"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"}

R-code:

library(RJSONIO)
json <- fromJSON(file_path_for_the_above_data, nullValue = NA)
dat <- lapply(json, function(j) {
as.data.frame(replace(j, sapply(j, is.list), NA))
})

This is where the error occurs.

Error in replace(j, sapply(j, is.list), NA) : invalid subscript type 'list'

Thank you.

Community
  • 1
  • 1
shanky_thebearer
  • 233
  • 1
  • 11

1 Answers1

0

The issue is with the wrong format of JSON, fixing the JSON array basically did the trick.

shanky_thebearer
  • 233
  • 1
  • 11