?
Solved

Help parsing and reading a BEA JSON file with jQuery getJSON

Posted on 2014-10-13
4
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 600 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 1400 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

762 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