3

I have an external URL for a JSON file which is hosted on another domain (not mine). Is it possible to parse this information with javascript only? Here is a sample of the JSON data. I only want to get "q" values.

[{"url":"http://website.com/?q=who+is+ip+search","q":"who is ip search"},{"url":"http://website.com/?q=eclipse+visual+editor","q":"eclipse visual editor"},{"url":"http://website.com/?q=partition+recovery","q":"partition recovery"},{"url":"http://www.website.com/?q=katzenfurz","q":"katzenfurz"},{"url":"http://website.com/?q=rtfm","q":"rtfm"},{"url":"http://website.com/?q=Google+ist+Dein+Freund","q":"Google ist Dein Freund"}]
Rustam
  • 1,875
  • 2
  • 16
  • 33
bammab
  • 2,543
  • 7
  • 25
  • 28
  • 1
    Just to add some history, JSON was originally used soley in JavaScript and means JavaScript Object Notation. So yes, JSON should be parseable with JavaScript. – kzh Nov 20 '11 at 14:13
  • possible duplicate of [Serializing to JSON in jQuery](http://stackoverflow.com/questions/191881/serializing-to-json-in-jquery) – outis Dec 26 '11 at 10:19
  • I think [this site](http://www.json.org/js.html) could be what you are looking for. – Josh Mein Nov 20 '11 at 12:50
  • Check [here](http://www.json.org/js.html). Or [here](http://www.hunlock.com/blogs/Mastering_JSON_%28_JavaScript_Object_Notation_%29) Both have good explanation and samples. – Çağdaş Nov 20 '11 at 12:49

3 Answers3

6

Browsers have native parsing methods -> JSON.parse() and JSON.stringify()

There are also several libraries that add the ability to parse JSON ...

Eval is sometimes used directly within JavaScript - but there are often security concerns when using this method -> http://en.wikipedia.org/wiki/JSON#JavaScript_eval.28.29

Manse
  • 37,765
  • 10
  • 83
  • 108
0

Yes, there is a built-in JSON.parse() function. Just pass the string to the function.

var obj = JSON.parse( data );

Live demo: http://jsfiddle.net/h4XTP/

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
  • @bammab No, you have to retrieve the JSON string first... (via Ajax, of course) – Šime Vidas Nov 20 '11 at 12:56
  • ok i wasnt sure about that part that's what i need i i wll search for documentation thank you – bammab Nov 20 '11 at 12:59
  • i looked at that for jquery but i need javascript because i want to use this for a chrome extension and i dont know if you can use jquery for it – bammab Nov 20 '11 at 13:09
  • @bammab If it's for a Chrome extension, you can safely use the `XMLHttpRequest` object to fetch the JSON data... – Šime Vidas Nov 20 '11 at 13:29
0

JSON you know is JavaScript object; yes you can parse it in JS. Though as you have remote server as data publisher, you have to configure that server for a callback function. To make the remote request, you insert a new script tag into your page, which will allow you to specify a remote URL. The reponse back will load a JSON object as a parameter of the callback function you specified in the request.

Once read somewhere. Hope it helped.