1

I'm just beginning to use the ONS API, and have managed to request specific data from R using rjstat::fromjsonstat to generate a nested list.

I'm aware that json data is not ordered, but I cannot see any indexing with which to recreate a data frame.

For example here is what happens when I pull population by parliamentary constituency:

require(rjstat)

http://www.ons.gov.uk/ons/api/data/dataset/KS101EW.json?context=Census&jsontype=json-stat&apikey=AG8Xr72lwm&geog=2011PCONH&totals=

Domain <- 'http://www.ons.gov.uk/ons/api/data/dataset/'

Dataset <- 'KS101EW.json'

Context <- '?context=Census&jsontype=json-stat'

API_key <- '&apikey=##########'

Geography <- '&geog=2011PCONH'

Slicing <- NULL

Paging <- '&totals=false'

ONS_URL <- paste(Domain,Dataset,Context,API_key,Geography,Slicing,Paging,sep='')

ONS_JSON <- fromJSONstat(ONS_URL)

ONS_JSON$`Usual resident population`[1:10,c('2011 Westminster Parliamentary Constituency Hierarchy','value')]

Output:

2011 Westminster Parliamentary Constituency Hierarchy    value
1                                                Watford 56075912
2                                                Watford 27573376
3                                                Watford 28502536
4                                                Waveney 53012456
5                                                Waveney 26069148
6                                                Waveney 26943308
7                         Washington and Sunderland West  3063456
8                         Washington and Sunderland West  1504228
9                         Washington and Sunderland West  1559228
10                                     England and Wales  2596886
r.bot
  • 5,309
  • 1
  • 34
  • 45
  • I'm not sure what you're trying to accomplish? `ONS_JSON$\`Usual resident population\`` is a `data.frame` and it has a `Time` field. – cocquemas Oct 29 '15 at 17:40
  • I'm quite possibly just being fairly stupid. More than quite possibly. – Jody Aberdein Oct 29 '15 at 18:08
  • Actually perhaps not. It is indeed a data frame, but the population values are not correct for the areas. See that Watford is given population of 56 million whereas England & Wales is 2.5 million. i.e. the df has lost its order. Presumably the json query gets data from the API in whatever order is convenient, and sticks it in the frame? – Jody Aberdein Oct 29 '15 at 18:11
  • It does look like the "value" field is not the population. You can try `r <- fromJSON(ONS_URL)` to examine the actual object being returned. The `category$index$E92000001` is `1`, which should match the first value of 56m ( `category$label$E92000001` is "England"). Maybe submit a bug report on the [rjstat GitHub page](https://github.com/ajschumacher/rjstat) to see if they know what's going on? – cocquemas Oct 29 '15 at 19:10
  • This issue has been resolved by updating to the latest version of rjstat - 0.2.1, thanks to @hmalmedal over at github! – Jody Aberdein Oct 30 '15 at 13:34

0 Answers0