0

I am having a heck of a time reading the following JSON into an R dataframe from a text file called "D:/myjsonShort" on Windows 10. I have tried using the RJSONIO,jsonlite, and rjson packages with the fromJSON functions, but can't seem to read this in. I want to read this so that my data frame will contain variables: "volumne", "timestamp", "high", "low", 'etc.

[{u'volume': u'0', u'timestamp': Decimal('0'), u'high': u'0', u'low': u'0', u'quoteVolume': u'0', u'close': u'0', u'weightedAverage': u'0', u'open': u'0', u'market': u'POLO-BTC_REP'}, 
{u'volume': u'0.02202444', u'timestamp': Decimal('1494050400'), u'high': u'0.01101223', u'low': u'0.01101222', u'quoteVolume': u'2', u'close': u'0.01101223', u'weightedAverage': u'0.01101222', u'open': u'0.01101222', u'market': u'POLO-BTC_REP'}, 
{u'volume': u'0.00363405', u'timestamp': Decimal('1494050700'), u'high': u'0.0110123', u'low': u'0.0110123', u'quoteVolume': u'0.33', u'close': u'0.0110123', u'weightedAverage': u'0.0110123', u'open': u'0.0110123', u'market': u'POLO-BTC_REP'}]

Here is some code I tried, but this just ends up returning an empty list of strings:

library(RJSONIO)
a<-fromJSON("D:/myjsonShort.txt")
a

and this returns:

[[1]]
[[1]]$`'volume`
NULL

[[1]]$`'timestamp`
NULL

[[1]]$`'high`
NULL

[[1]]$`'low`
NULL

[[1]]$`'quoteVolume`
NULL

[[1]]$`'close`
NULL

[[1]]$`'weightedAverage`
NULL

[[1]]$`'open`
NULL

[[1]]$`'market`
NULL


[[2]]
[[2]]$`'volume`
NULL

[[2]]$`'timestamp`
NULL

[[2]]$`'high`
NULL

[[2]]$`'low`
NULL

[[2]]$`'quoteVolume`
NULL

[[2]]$`'close`
NULL

[[2]]$`'weightedAverage`
NULL

[[2]]$`'open`
NULL

[[2]]$`'market`
NULL


[[3]]
[[3]]$`'volume`
NULL

[[3]]$`'timestamp`
NULL

[[3]]$`'high`
NULL

[[3]]$`'low`
NULL

[[3]]$`'quoteVolume`
NULL

[[3]]$`'close`
NULL

[[3]]$`'weightedAverage`
NULL

[[3]]$`'open`
NULL

[[3]]$`'market`
NULL

Any ideas how to fix this?

StatsStudent
  • 1,384
  • 2
  • 10
  • 28
  • how did you generate `myjsonShort.txt` ? it's not valid JSON. – SymbolixAU Jun 18 '17 at 07:42
  • @SymbolixAU Why do you say it's not valid? It looks valid to me (although I'm probably missing something). It was originally generated using boto3 in python extracting data from dynamodb on AWS and then written to a text file. – StatsStudent Jun 18 '17 at 07:43
  • I also found another similar post, but the responses here didn't seem to help me because my JSON seems to have UTF encoding: https://stackoverflow.com/questions/16947643/getting-imported-json-data-into-a-data-frame-in-r – StatsStudent Jun 18 '17 at 07:54
  • because all the `u'volume': u'0'` stuff is invalid. You can test it at [jsonlint.com](https://jsonlint.com/) – SymbolixAU Jun 18 '17 at 07:56
  • Right. That's what I was thinking too. I think I know how to handle that. Thanks for your help, @SymbolixAU. – StatsStudent Jun 18 '17 at 08:46
  • 1
    I have found that windows 10 uses UTF-16LE and jsonlite wants UTF-8. You can convert like this: `jsondata <- iconv(data$value, from = "UTF-16LE", to = "UTF-8")` – Marichyasana Jun 25 '17 at 11:45

0 Answers0