Andrew Pollack's Blog

Technology, Family, Entertainment, Politics, and Random Noise

Have you ever needed to unescape a bunch of JSON data at once?

By Andrew Pollack on 07/19/2013 at 02:54 PM EDT

I don't really know if this is "a thing" for anyone else, but I have a whole bunch of JSON data that comes down from a provider as a multi-level object and all the string data is escaped (as it should be). I got tired having to unescape() each string value as I went to use it, so I went looking for something someone else had posted online about doing a "deep unescape" and didn't find it.

Given that I didn't find other people asking about it or getting answers, I have to assume that:

a) There's a parameter somewhere I've missed that does it for you
b) There's a function out there that everyone else knows about but me
c) Nobody else has bothered to do this, and I'm just brilliant.

Option "C" seems particularly unlikely given that I don't do anywhere near as much javascript as many other people, but if this turns out to be useful to anyone, here you go.....



function deepUnescapeJSON(object) {
  /* created by Andrew Pollack 7/19/2013 */
  /* This is a recursive function which will traverse a JSON object */
  /* and unescape all the string properties. I have no idea if it */
  /* would work on other objects, or even all kinds of JSON data */
  /* use it at your own risk.  It seems to work for me. */
  var val ;
  for(var propName in object){
    val = object[propName]; // just in case there's some other type of property
    if(typeof( object[propName] ) == "string") val = unescape(object[propName]);
    if(typeof( object[propName]) == "object") val = deepUnescapeJSON(object[propName]);
    object[propName] = val ;
    }
  return object;
}

** Yes, I know this could be a little more efficient if I used a "switch" statement and a default case option to keep the original value instead of copying the objects around in memory so much. I can't be bothered to do it though. This works, and unless the objects get truly massive, nobody is going to notice the difference --- especially since objects are copied by reference in javascript anyway.

There are  - loading -  comments....

re: Have you ever needed to unescape a bunch of JSON data at once?By Randy Loeb on 10/01/2014 at 01:29 PM EDT
Thanks, just what I needed. Used underscore.js's _.unenescape() but otherwise
worked for me.
re: Have you ever needed to unescape a bunch of JSON data at once?By Alex on 05/11/2018 at 03:19 PM EDT
Thanks! I replaced unescape() with _unescape() from lodash.


Other Recent Stories...

  1. 09/04/2018With two big projects on hold, I suddenly find myself very available for new short and long term projects. In twenty five years, I don't think I've ever written an entry like this, but if you need the kind of work I do now would be a great time to get in touch. Both of the big projects I had lined up for late summer and early fall have been placed on hold and will be that way for a while. With the kids now all off at college and careers, I'm open to more travel than such than I have been in decades, but unless something else comes along, I'll be here working on updates to Second Signal and other things that ...... 
  2. 07/13/2018Who is HCL and why is it a good thing that they are now the ones behind Notes and Domino?We need to address some biases here. IBM has made a deal under which the Notes & Domino software and intellectual property is now being developed and maintained by HCL America. HCL America is part of the very large "HCL Technologies" company that has grown from its roots in India to become an 8 Billion Dollar company with a global presence in the IT Industry. You could be excused for initially believing, as many people do when they hear this, that "they've outsourced the code to India where they'll milk it ...... 
  3. 03/21/2018Domino Apps on IOS is a Game Changer. Quit holding back.BOOM. This will be as important for the platform as Traveler. If your company has ditched Notes and Domino, I feel sorry for you. For companies that do use Notes/Domino this is a game changer and Apple should be paying attention. Here's why: There are hundreds of little Notes client applications you'd never spend the time and money to build and deploy for your internal user base on IOS that we use Notes for all the time (those of us still using it). Now, those are suddenly ALL available on the iPad. ...... 
  4. 02/15/2018Andrew’s Proposed Gun Laws 
  5. 05/05/2016Is the growing social-sourced economy the modern back door into socialism? 
  6. 04/20/2016Want to be whitelisted? Here are some sensible rules for web site advertising 
  7. 12/30/2015Fantastic new series on Syfy called “The Expanse” – for people who love traditional science fiction 
  8. 10/20/2015My suggestion is to stay away from PayAnywhere(dot)com  
  9. 08/07/2015Here is one for you VMWARE gurus - particularly if you run ESXi without fancy drive arrays 
  10. 08/06/2015The Killer of Orphans (Orphan Documents) 
Click here for more articles.....


pen icon Comment Entry
Subject
Your Name
Homepage
*Your Email
* Your email address is required, but not displayed.
 
Your thoughts....
 
Remember Me  

Please wait while your document is saved.