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"}]
4
  • Check here. Or here Both have good explanation and samples. Commented Nov 20, 2011 at 12:49
  • I think this site could be what you are looking for. Commented Nov 20, 2011 at 12:50
  • 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. Commented Nov 20, 2011 at 14:13
  • possible duplicate of Serializing to JSON in jQuery Commented Dec 26, 2011 at 10:19

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Which modern browsers don't implement JSON.parse()? I don't think there are any...
@ŠimeVidas perhaps its all then ... i assumed some mobile browsers didnt implement it ... maybe wrongly
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/

4 Comments

@bammab No, you have to retrieve the JSON string first... (via Ajax, of course)
ok i wasnt sure about that part that's what i need i i wll search for documentation thank you
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 If it's for a Chrome extension, you can safely use the XMLHttpRequest object to fetch the JSON data...
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.