132

I'm trying to parse a bit.ly JSON response in javascript.

I get the JSON via XmlHttpRequest.

var req = new XMLHttpRequest;  
req.overrideMimeType("application/json");  
req.open('GET', BITLY_CREATE_API + encodeURIComponent(url)
          + BITLY_API_LOGIN, true);  
var target = this;  
req.onload  = function() {target.parseJSON(req, url)};  
req.send(null);

parseJSON: function(req, url) {  
if (req.status == 200) {  
    var jsonResponse = req.responseJSON;  
    var bitlyUrl = jsonResponse.results[url].shortUrl;  
}

I do this in a firefox addon. When I run I get the error "jsonResponse is undefined" for the line var bitlyUrl = jsonResponse.results[url].shortUrl;. Am I doing anything wrong in parsing JSON here? Or what is wrong with this code?

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
chanux
  • 1,829
  • 4
  • 16
  • 20

5 Answers5

283

New ways I: fetch

TL;DR I'd recommend this way as long as you don't have to send synchronous requests or support old browsers.

A long as your request is asynchronous you can use the Fetch API to send HTTP requests. The fetch API works with promises, which is a nice way to handle asynchronous workflows in JavaScript. With this approach you use fetch() to send a request and ResponseBody.json() to parse the response:

fetch(url)
  .then(function(response) {
    return response.json();
  })
  .then(function(jsonResponse) {
    // do something with jsonResponse
  });

Compatibility: The Fetch API is not supported by IE11 as well as Edge 12 & 13. However, there are polyfills.

New ways II: responseType

As Londeren has written in his answer, newer browsers allow you to use the responseType property to define the expected format of the response. The parsed response data can then be accessed via the response property:

var req = new XMLHttpRequest();
req.responseType = 'json';
req.open('GET', url, true);
req.onload  = function() {
   var jsonResponse = req.response;
   // do something with jsonResponse
};
req.send(null);

Compatibility: responseType = 'json' is not supported by IE11.

The classic way

The standard XMLHttpRequest has no responseJSON property, just responseText and responseXML. As long as bitly really responds with some JSON to your request, responseText should contain the JSON code as text, so all you've got to do is to parse it with JSON.parse():

var req = new XMLHttpRequest();
req.overrideMimeType("application/json");
req.open('GET', url, true);
req.onload  = function() {
   var jsonResponse = JSON.parse(req.responseText);
   // do something with jsonResponse
};
req.send(null);

Compatibility: This approach should work with any browser that supports XMLHttpRequest and JSON.

JSONHttpRequest

If you prefer to use responseJSON, but want a more lightweight solution than JQuery, you might want to check out my JSONHttpRequest. It works exactly like a normal XMLHttpRequest, but also provides the responseJSON property. All you have to change in your code would be the first line:

var req = new JSONHttpRequest();

JSONHttpRequest also provides functionality to easily send JavaScript objects as JSON. More details and the code can be found here: http://pixelsvsbytes.com/2011/12/teach-your-xmlhttprequest-some-json/.

Full disclosure: I'm the owner of Pixels|Bytes. I thought that my script was a good solution for the original question, but it is rather outdated today. I do not recommend to use it anymore.

Torben
  • 6,317
  • 1
  • 33
  • 32
  • 10
    +1; IMO this was the real answer to the question - no jQuery just plain old vanilla ```XMLHttpRequest```; just what the question was about. – Fergus In London Jan 08 '13 at 01:07
  • There`s a jquery version too. If you are getting crossbrowser issue`s try it, usually framework`s handle these problems better: http://api.jquery.com/jquery.parsejson/ – Renato Probst Jan 29 '14 at 17:01
  • 1
    Four years later and this is still helping people. :) Linking to the blog is fine IMO, since it really is the full answer to the question with sample code and a download. Thank you! – user1094821 Nov 24 '15 at 19:06
  • 1
    "Use a new library" is not as helpful as one might think. – Sean Munson Jan 28 '20 at 18:10
  • @GrunionShaftoe Could you please explain, what do mean? I'm not suggesting to use a new library. My recommended solution `fetch` is standard JavaScript. – Torben Jan 29 '20 at 19:46
  • I find the `fetch(url).then(...).then(...)` notation quite cumbersome. An alternative is to create an `async` function and use `await`. A one-liner to answer the original question is `async () => { responseJson = (await fetch(url)).json() /* do something with responseJson */ }`. Alternatively, you can do something like `response = await fetch(url)` if you need the full response to check for example `response.ok` and the json can be extracted with `response.json()`. The caveat to `async` functions is that you are required to return a `Promise` if you have a return value. – turbo_sheep Mar 08 '20 at 13:24
36

You can simply set xhr.responseType = 'json';

const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts/1');
xhr.responseType = 'json';
xhr.onload = function(e) {
  if (this.status == 200) {
    console.log('response', this.response); // JSON response  
  }
};
xhr.send();
  

Documentation for responseType

Londeren
  • 3,202
  • 25
  • 26
3

Note: I've only tested this in Chrome.

it adds a prototype function to the XMLHttpRequest .. XHR2,

in XHR 1 you probably just need to replace this.response with this.responseText

Object.defineProperty(XMLHttpRequest.prototype,'responseJSON',{value:function(){
 return JSON.parse(this.response);
},writable:false,enumerable:false});

to return the json in xhr2

xhr.onload=function(){
 console.log(this.responseJSON());
}

EDIT

If you plan to use XHR with arraybuffer or other response types then you have to check if the response is a string.

in any case you have to add more checks e.g. if it's not able to parse the json.

Object.defineProperty(XMLHttpRequest.prototype,'responseJSON',{value:function(){
 return (typeof this.response==='string'?JSON.parse(this.response):this.response);
},writable:false,enumerable:false});
Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31
cocco
  • 16,442
  • 7
  • 62
  • 77
  • 2
    I would define a [getter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) instead of a function attribute if I were you. Just replace `value` with `get` in the object passed to `Object.defineProperty`, and you can use `responseJSON` like you would any other response variable. – wizzwizz4 Sep 16 '16 at 16:35
2

I think you have to include jQuery to use responseJSON.

Without jQuery, you could try with responseText and try like eval("("+req.responseText+")");

UPDATE:Please read the comment regarding eval, you can test with eval, but don't use it in working extension.

OR

use json_parse : it does not use eval

YOU
  • 120,166
  • 34
  • 186
  • 219
  • 5
    For a Firefox addon running with chrome privs, don't try to eval anything you get from an outside source. Instead, use JSON.parse (at least in FF 3.5 and later). – Ben Combee Dec 29 '09 at 06:25
2

Use nsIJSON if this is for a FF extension:

var req = new XMLHttpRequest;
req.overrideMimeType("application/json");
req.open('GET', BITLY_CREATE_API + encodeURIComponent(url) + BITLY_API_LOGIN, true);
var target = this;
req.onload = function() {target.parseJSON(req, url)};
req.send(null);

parseJSON: function(req, url) {
if (req.status == 200) {
  var jsonResponse = Components.classes["@mozilla.org/dom/json;1"]
      .createInstance(Components.interfaces.nsIJSON.decode(req.responseText);
  var bitlyUrl = jsonResponse.results[url].shortUrl;
}

For a webpage, just use JSON.parse instead of Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON.decode

erikvold
  • 15,988
  • 11
  • 54
  • 98