Solved

Help parsing and reading a BEA JSON file with jQuery getJSON

Posted on 2014-10-13
4
233 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

760 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

21 Experts available now in Live!

Get 1:1 Help Now