How to parse json response from Bigquery results?

I tried a javascript sample code to call the Google bigQuery API ( https://developers.google.com/bigquery/docs/authorization#client-side-javascript )

Js:

function runQuery() { var request = gapi.client.bigquery.jobs.query({ 'projectId': project_id, 'timeoutMs': '30000', 'query': 'SELECT TOP(repository_language, 5) as language, COUNT(*) as count FROM [publicdata:samples.github_timeline] WHERE repository_language != "";' }); request.execute(function(response) { console.log(response); var results = response.result.rows ; $('#result_box').html(JSON.stringify(results, null)); }); } 

Above a large query returns :

 [{"f":[{"v":"JavaScript"},{"v":"949899"}]},{"f":[{"v":"Ruby"},{"v":"640659"}]},{"f":[{"v":"Java"},{"v":"568202"}]},{"f":[{"v":"Python"},{"v":"484852"}]},{"f":[{"v":"PHP"},{"v":"453830"}]}] 

Please help me parse the values ​​from the above results in JSON format?

 {"JavaScript": "949899", "Ruby": "640659", "Java": "568202", "Python": "484852", "PHP": "453830" } 
+4
source share
3 answers

Eval is a security risk.

 var text = '[{"f":[{"v":"JavaScript"},{"v":"949899"}]},{"f":[{"v":"Ruby"},{"v":"640659"}]},{"f":[{"v":"Java"},{"v":"568202"}]},{"f":[{"v":"Python"},{"v":"484852"}]},{"f":[{"v":"PHP"},{"v":"453830"}]}]'; myData = JSON.parse(text); alert(myData[4].f[0].v);​ 
+3
source

http://jsfiddle.net/jv7vm/

 var a='[{"f":[{"v":"JavaScript"},{"v":"949899"}]},{"f":[{"v":"Ruby"},{"v":"640659"}]},{"f":[{"v":"Java"},{"v":"568202"}]},{"f":[{"v":"Python"},{"v":"484852"}]},{"f":[{"v":"PHP"},{"v":"453830"}]}]'; var evala=eval('('+a+')'); for(i=0;i<evala.length;i++) { document.write(evala[i].f[0].v+' '+evala[i].f[1].v+'<br>'); } 
0
source

Got a response below javascript

 var total = response.result.totalRows; var data = []; for(i=0; i < total; i++){ data[i]= [ response.rows[i].f[0]["v"], response.rows[i].f[1]["v"] ]; } console.log(data); 
0
source

All Articles