Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Deeply Nested JSON Structures in R Using RJSONIO

Tags:

r

I suspect I'm missing something obvious here, but how do I parse deeply nested structures in R using RJSONIO?

For example - suppose I directly want to reference $familyName in results.data.json$MRData$RaceTable$Races[[1]]$Results[[8]]$Driver as grabbed using the following snippet:

require(RJSONIO)
resultsURL='http://ergast.com/api/f1/2012/1/results.json'
results.data.json=fromJSON(resultsURL)

RJSONIO doesn't appear to want to parse the ..$Results[[i]] data as structured elements?

like image 776
psychemedia Avatar asked Sep 16 '12 18:09

psychemedia


2 Answers

require(RJSONIO)

somedata1<-list(a=1,b='w')
fromJSON(toJSON(somedata1))
# $a
# [1] 1

# $b
# [1] "w"

somedata2<-list(a=1,b=2)
fromJSON(toJSON(somedata2))
# a b 
# 1 2 

somedata3<-list(a='v',b='w')
fromJSON(toJSON(somedata3))
#  a   b 
# "v" "w" 

fromJSON(toJSON(somedata3),simplify=StrictNumeric)
# $a
# [1] "v"

# $b
# [1] "w"

fromJSON(toJSON(somedata2),simplify=FALSE)
# $a
# [1] 1

# $b
# [1] 2

fromJSON(toJSON(somedata3),simplifyWithNames = FALSE)
# $a
# [1] "v"

# $b
# [1] "w"


fromJSON(toJSON(somedata2),simplifyWithNames = FALSE)
# $a
# [1] 1

# $b
# [1] 2

from the examples above by default RJSON simplifies "collections/arrays of homogeneous scalar elements to R vectors". This simplification can be controlled using simplify or simplifyWithNames. In your example you can do any of the following to access the element you want:

require(RJSONIO)
resultsURL='http://ergast.com/api/f1/2012/1/results.json'
results.data.json=fromJSON(resultsURL)
results.data.json$MRData$RaceTable$Races[[1]]$Results[[8]]$Driver['familyName']
# familyName 
#    "Pérez" 

results.data.json=fromJSON(resultsURL,simplify=FALSE)
results.data.json$MRData$RaceTable$Races[[1]]$Results[[8]]$Driver$familyName
# [1] "Pérez"

results.data.json=fromJSON(resultsURL,simplify=StrictNumeric)
results.data.json$MRData$RaceTable$Races[[1]]$Results[[8]]$Driver$familyName
# [1] "Pérez"

results.data.json=fromJSON(resultsURL,simplifyWithNames = FALSE)
results.data.json$MRData$RaceTable$Races[[1]]$Results[[8]]$Driver$familyName
# [1] "Pérez"
like image 169
user1609452 Avatar answered Sep 21 '22 13:09

user1609452


The jsonlite package is a fork of RJSONIO which tries to use a smarter mapping between R and JSON structures. I think this might make your life easier:

> x = fromJSON('http://ergast.com/api/f1/2012/1/results.json')
> x$RaceTable$Races$MRData$Results[[1]]$Driver
             driverId code                                                   url
1              button  BUT            http://en.wikipedia.org/wiki/Jenson_Button
2              vettel  VET         http://en.wikipedia.org/wiki/Sebastian_Vettel
3            hamilton  HAM           http://en.wikipedia.org/wiki/Lewis_Hamilton
4              webber  WEB              http://en.wikipedia.org/wiki/Mark_Webber
5              alonso  ALO          http://en.wikipedia.org/wiki/Fernando_Alonso
6           kobayashi  KOB          http://en.wikipedia.org/wiki/Kamui_Kobayashi
7           raikkonen  RAI http://en.wikipedia.org/wiki/Kimi_R%C3%A4ikk%C3%B6nen
8               perez  PER        http://en.wikipedia.org/wiki/Sergio_P%C3%A9rez
9           ricciardo  RIC         http://en.wikipedia.org/wiki/Daniel_Ricciardo
10              resta  DIR            http://en.wikipedia.org/wiki/Paul_di_Resta
11             vergne  VER    http://en.wikipedia.org/wiki/Jean-%C3%89ric_Vergne
12            rosberg  ROS             http://en.wikipedia.org/wiki/Nico_Rosberg
13          maldonado  MAL         http://en.wikipedia.org/wiki/Pastor_Maldonado
14              glock  GLO               http://en.wikipedia.org/wiki/Timo_Glock
15                pic  PIC              http://en.wikipedia.org/wiki/Charles_Pic
16        bruno_senna  SEN              http://en.wikipedia.org/wiki/Bruno_Senna
17              massa  MAS             http://en.wikipedia.org/wiki/Felipe_Massa
18         kovalainen  KOV        http://en.wikipedia.org/wiki/Heikki_Kovalainen
19             petrov  PET            http://en.wikipedia.org/wiki/Vitaly_Petrov
20 michael_schumacher  MSC       http://en.wikipedia.org/wiki/Michael_Schumacher
21           grosjean  GRO          http://en.wikipedia.org/wiki/Romain_Grosjean
22         hulkenberg  HUL     http://en.wikipedia.org/wiki/Nico_H%C3%BClkenberg
23               rosa  DLR         http://en.wikipedia.org/wiki/Pedro_de_la_Rosa
24        karthikeyan  KAR       http://en.wikipedia.org/wiki/Narain_Karthikeyan
   givenName  familyName dateOfBirth nationality
1     Jenson      Button  1980-01-19     British
2  Sebastian      Vettel  1987-07-03      German
3      Lewis    Hamilton  1985-01-07     British
4       Mark      Webber  1976-08-27  Australian
5   Fernando      Alonso  1981-07-29     Spanish
6      Kamui   Kobayashi  1986-09-13    Japanese
7       Kimi   Räikkönen  1979-10-17     Finnish
8     Sergio       Pérez  1990-01-26     Mexican
9     Daniel   Ricciardo  1989-07-01  Australian
10      Paul    di Resta  1986-04-16    Scottish
11 Jean-Éric      Vergne  1990-04-25      French
12      Nico     Rosberg  1985-06-27      German
13    Pastor   Maldonado  1985-03-09  Venezuelan
14      Timo       Glock  1982-03-18      German
15   Charles         Pic  1990-02-15      French
16     Bruno       Senna  1983-10-15   Brazilian
17    Felipe       Massa  1981-04-25   Brazilian
18    Heikki  Kovalainen  1981-10-19     Finnish
19    Vitaly      Petrov  1984-09-08     Russian
20   Michael  Schumacher  1969-01-03      German
21    Romain    Grosjean  1986-04-17      French
22      Nico  Hülkenberg  1987-08-19      German
23     Pedro  de la Rosa  1971-02-24     Spanish
24    Narain Karthikeyan  1977-01-14      Indian
like image 26
Jeroen Ooms Avatar answered Sep 20 '22 13:09

Jeroen Ooms