Solved

Help parsing and reading a BEA JSON file with jQuery getJSON

Posted on 2014-10-13
4
239 Views
Last Modified: 2014-10-20
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
Comment
Question by:lvmllc
  • 2
4 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 150 total points
ID: 40378722
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
 

Author Comment

by:lvmllc
ID: 40379102
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
 
LVL 82

Expert Comment

by:leakim971
ID: 40379349
You must have a cjs function?
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 350 total points
ID: 40379358
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iframe detection of parent window scale 20 59
Javascript closure 1 34
ngStyle with an input variable doesn't work 14 30
Regular Expression 1-100 with two digits 15 21
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now