Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Help parsing and reading a BEA JSON file with jQuery getJSON

I am trying to access data from the BEA using their API.

The URL requires a key, but here it is with the key left out

http://www.bea.gov/api/data/?&UserID=MYKEY&method=GETDATA&datasetname=RegionalData&KeyCode=PCPI_CI&GeoFIPS=19001,19003,19005,19007,19009&Year=2012&ResultFormat=json&jsonp=cjs

The result is

cjs({"BEAAPI":{"Request":{"RequestParam":[{"ParameterName":"RESULTFORMAT","ParameterValue":"JSON"},{"ParameterName":"YEAR","ParameterValue":"2012"},{"ParameterName":"USERID","ParameterValue":"005E537D-F683-457E-A70B-128195319CE1"},{"ParameterName":"JSONP","ParameterValue":"CJS"},{"ParameterName":"GEOFIPS","ParameterValue":"19001,19003,19005,19007,19009"},{"ParameterName":"KEYCODE","ParameterValue":"PCPI_CI"},{"ParameterName":"DATASETNAME","ParameterValue":"REGIONALDATA"},{"ParameterName":"METHOD","ParameterValue":"GETDATA"}]},"Results": {"Statistic":"Per capita personal income","UnitOfMeasure":"dollars","PublicTable":"CA1-3 Personal income summary","UTCProductionTime":"2014-10-13T21:46:25.680","NoteRef":"2" ,"Dimensions": [{"Ordinal":"1","Name":"GeoFips","DataType":"string","IsValue":"0"},{"Ordinal":"2","Name":"GeoName","DataType":"string","IsValue":"0"},{"Ordinal":"3","Name":"Code","DataType":"string","IsValue":"0"},{"Ordinal":"4","Name":"TimePeriod","DataType":"string","IsValue":"0"},{"Ordinal":"5","Name":"CL_UNIT","DataType":"string","IsValue":"0"},{"Ordinal":"6","Name":"UNIT_MULT","DataType":"numeric","IsValue":"0"},{"Ordinal":"7","Name":"DataValue","DataType":"numeric","IsValue":"1"}],"Data": [{"GeoFips":"19001","GeoName":"Adair, IA ","Code":"PCPI_CI","TimePeriod":"2012","CL_UNIT":"USD","UNIT_MULT":"0","DataValue":"40008"},{"GeoFips":"19003","GeoName":"Adams, IA ","Code":"PCPI_CI","TimePeriod":"2012","CL_UNIT":"USD","UNIT_MULT":"0","DataValue":"53297"},{"GeoFips":"19005","GeoName":"Allamakee, IA ","Code":"PCPI_CI","TimePeriod":"2012","CL_UNIT":"USD","UNIT_MULT":"0","DataValue":"37269"},{"GeoFips":"19007","GeoName":"Appanoose, IA ","Code":"PCPI_CI","TimePeriod":"2012","CL_UNIT":"USD","UNIT_MULT":"0","DataValue":"31384"},{"GeoFips":"19009","GeoName":"Audubon, IA ","Code":"PCPI_CI","TimePeriod":"2012","CL_UNIT":"USD","UNIT_MULT":"0","DataValue":"50094"}],"Notes": [{"NoteRef":"2","NoteText":"Per capita personal income was computed using Census Bureau midyear population estimates. Estimates for 2010-2012 reflect county population estimates available as of March 2013."},{"NoteRef":"Note--","NoteText":"All dollar estimates are in current dollars (not adjusted for inflation)."},{"NoteRef":" ","NoteText":"Last updated: May 30, 2014-- revised estimates for 1969-2000."}]}}})


How would I access it so I could print to the console the data value for Adams, IA  - which would be 53297  - note printing all to console is fine - filtering is not part of this question.

So basically I am just not sure how to access it with jQuery getJSON
0
lvmllc
Asked:
lvmllc
  • 2
2 Solutions
 
leakim971PluritechnicianCommented:
test page : http://jsfiddle.net/3a18jkrn/

function cjs(json) {
    var data = json.BEAAPI.Results.Data;
    for(var i=0;i<data.length;i++) {
        if(data[i].GeoName == "Adams, IA ") { // be careful you've an extra space !!!
//            console.log(data[i].DataValue);
            alert(data[i].DataValue);
        }
    }
}

Open in new window

0
 
lvmllcAuthor Commented:
I see how this works with the result when on the same page, but I still have an issue with how I retrieve it using the URL
 
http://www.bea.gov/api/data/?&UserID=MYKEY&method=GETDATA&datasetname=RegionalData&KeyCode=PCPI_CI&GeoFIPS=19001,19003,19005,19007,19009&Year=2012&ResultFormat=json&jsonp=cjs
0
 
leakim971PluritechnicianCommented:
You must have a cjs function?
0
 
Albert Van HalenAnalyst developerCommented:
Define your callback method (in your case 'cjs') in your page. After that you can retrieve the data by using $.ajax and set the dataType to jsonp. getJSON won't work because of CORS is disabled by default.

Take a look here : http://jsfiddle.net/qb12j9e8/
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now