0

So I have a json string as shown below:

 {"count":1,"items":[{"rating":"teens","videos":[{"width":1280,"link":"http://192.168.195.86:1081/playback/play/99e2399e-368b","dar_image":1.7777777778,"language":"eng","duration":5166,"type":"main","id":"99e2399e-368b","dar_frame":1.7777777778,"height":720}],"trial":false,"metadata_language":"eng","year":2003,"images":[{"link":"http://192.168.195.86:1081/image/1675610d-e57e.jpg","type":"poster","id":"1675610d-e57e","orientation":"landscape"}],"id":"d0e9d382-e2d4","has_download_policy":true,"title":"Ice Bound","section":"hollywood","crew":[{"role":"director","name":"Roger Spottiswoode"}],"type":"movie","website_url":"http://192.168.195.86:1081/eng/movie/pkdaci6s-ice-bound","description":"Testin'g «ταБЬℓσ»: 1<{2 & 4+1}>3, now 20% off!@#$%^&*(). Another Testing String ","link":"http://192.168.195.86:1081/movie/d0e9d382-e2d4","slug":"pkdaci6s","categories":[{"link":"http://192.168.195.86:1081/category/ca8a9dce-514d","id":"ca8a9dce-514d","title":"Drama"}],"metadata_direction":"ltr","audio_languages":["eng"],"cast":[{"name":"Susan Sarandon"},{"name":"Aidan Devine"},{"name":"Cynthia Mace"}]}],"remaining":1}   

I need to convert this to a raw string format, where all the special characters and symbols get replaced with unicode or have escape characters.

 {"count":1,"items":[{"rating":"teens","videos":[{"width":1280,"link":"http:\/\/192.168.195.86:1081\/playback\/play\/99e2399e-368b","dar_image":1.7777777778,"language":"eng","duration":5166,"type":"main","id":"99e2399e-368b","dar_frame":1.7777777778,"height":720}],"trial":false,"metadata_language":"eng","year":2003,"images":[{"link":"http:\/\/192.168.195.86:1081\/image\/1675610d-e57e.jpg","type":"poster","id":"1675610d-e57e","orientation":"landscape"}],"id":"d0e9d382-e2d4","has_download_policy":true,"title":"Ice Bound","section":"hollywood","crew":[{"role":"director","name":"Roger Spottiswoode"}],"type":"movie","website_url":"http:\/\/192.168.195.86:1081\/eng\/movie\/pkdaci6s-ice-bound","description":"Testin\'g \u2xxx \u2xxx\u2xxx\u2xxx\u2xxx\u2xxx\u2xxx\u2xxx: 1<{2 & 4+1}>3, now 20% off!@#$%^&*(). Another Testing String ","link":"http:\/\/192.168.195.86:1081\/movie\/d0e9d382-e2d4","slug":"pkdaci6s","categories":[{"link":"http:\/\/192.168.195.86:1081\/category\/ca8a9dce-514d","id":"ca8a9dce-514d","title":"Drama"}],"metadata_direction":"ltr","audio_languages":["eng"],"cast":[{"name":"Susan Sarandon"},{"name":"Aidan Devine"},{"name":"Cynthia Mace"}]}],"remaining":1}   

How will I do this in javascript?
All pointers are welcome.


Example link http://api.icflix.com/tv/catalogue/movies?num=1

Navaneeth Sen
  • 6,315
  • 11
  • 53
  • 82

2 Answers2

1

Try this:

function escapeString(str) {
    var result = '',
        code;

    for (var i = 0; i < str.length; i++) {
        code = str.charCodeAt(i);
        if (code < 32 || code > 126) {
            result += '\\u2' + code;
        } else {
            result += str[i];
        }
    }
    return result;
}

var orig = '{"count":1,"items":[{"rating":"teens","videos":[{"width":1280,"link":"http://192.168.195.86:1081/playback/play/99e2399e-368b","dar_image":1.7777777778,"language":"eng","duration":5166,"type":"main","id":"99e2399e-368b","dar_frame":1.7777777778,"height":720}],"trial":false,"metadata_language":"eng","year":2003,"images":[{"link":"http://192.168.195.86:1081/image/1675610d-e57e.jpg","type":"poster","id":"1675610d-e57e","orientation":"landscape"}],"id":"d0e9d382-e2d4","has_download_policy":true,"title":"Ice Bound","section":"hollywood","crew":[{"role":"director","name":"Roger Spottiswoode"}],"type":"movie","website_url":"http://192.168.195.86:1081/eng/movie/pkdaci6s-ice-bound","description":"Testin\'g «ταБЬℓσ»: 1<{2 & 4+1}>3, now 20% off!@#$%^&*(). Another Testing String ","link":"http://192.168.195.86:1081/movie/d0e9d382-e2d4","slug":"pkdaci6s","categories":[{"link":"http://192.168.195.86:1081/category/ca8a9dce-514d","id":"ca8a9dce-514d","title":"Drama"}],"metadata_direction":"ltr","audio_languages":["eng"],"cast":[{"name":"Susan Sarandon"},{"name":"Aidan Devine"},{"name":"Cynthia Mace"}]}],"remaining":1} ';

result = escapeString(orig);
console.log(result);

Result:

{"count":1,"items":[{"rating":"teens","videos":[{"width":1280,"link":"http://192.168.195.86:1081/playback/play/99e2399e-368b","dar_image":1.7777777778,"language":"eng","duration":5166,"type":"main","id":"99e2399e-368b","dar_frame":1.7777777778,"height":720}],"trial":false,"metadata_language":"eng","year":2003,"images":[{"link":"http://192.168.195.86:1081/image/1675610d-e57e.jpg","type":"poster","id":"1675610d-e57e","orientation":"landscape"}],"id":"d0e9d382-e2d4","has_download_policy":true,"title":"Ice Bound","section":"hollywood","crew":[{"role":"director","name":"Roger Spottiswoode"}],"type":"movie","website_url":"http://192.168.195.86:1081/eng/movie/pkdaci6s-ice-bound","description":"Testin'g \u2171\u2964\u2945\u21041\u21068\u28467\u2963\u2187: 1<{2 & 4+1}>3, now 20% off!@#$%^&*(). Another Testing String ","link":"http://192.168.195.86:1081/movie/d0e9d382-e2d4","slug":"pkdaci6s","categories":[{"link":"http://192.168.195.86:1081/category/ca8a9dce-514d","id":"ca8a9dce-514d","title":"Drama"}],"metadata_direction":"ltr","audio_languages":["eng"],"cast":[{"name":"Susan Sarandon"},{"name":"Aidan Devine"},{"name":"Cynthia Mace"}]}],"remaining":1} 

Or this:

result = orig.split('').map(function(v) {
    var code = v.charCodeAt(0);
    if (code < 32 || code > 126) {
        return '\\u2' + code;
    } else {
        return v;
    }
}).join('');

console.log(result);

Same result:

{"count":1,"items":[{"rating":"teens","videos":[{"width":1280,"link":"http://192.168.195.86:1081/playback/play/99e2399e-368b","dar_image":1.7777777778,"language":"eng","duration":5166,"type":"main","id":"99e2399e-368b","dar_frame":1.7777777778,"height":720}],"trial":false,"metadata_language":"eng","year":2003,"images":[{"link":"http://192.168.195.86:1081/image/1675610d-e57e.jpg","type":"poster","id":"1675610d-e57e","orientation":"landscape"}],"id":"d0e9d382-e2d4","has_download_policy":true,"title":"Ice Bound","section":"hollywood","crew":[{"role":"director","name":"Roger Spottiswoode"}],"type":"movie","website_url":"http://192.168.195.86:1081/eng/movie/pkdaci6s-ice-bound","description":"Testin'g \u2171\u2964\u2945\u21041\u21068\u28467\u2963\u2187: 1<{2 & 4+1}>3, now 20% off!@#$%^&*(). Another Testing String ","link":"http://192.168.195.86:1081/movie/d0e9d382-e2d4","slug":"pkdaci6s","categories":[{"link":"http://192.168.195.86:1081/category/ca8a9dce-514d","id":"ca8a9dce-514d","title":"Drama"}],"metadata_direction":"ltr","audio_languages":["eng"],"cast":[{"name":"Susan Sarandon"},{"name":"Aidan Devine"},{"name":"Cynthia Mace"}]}],"remaining":1} 

The code considers anything below 32 and above 126 to be special characters. You need to update it for you scenario.

Will
  • 1,718
  • 3
  • 15
  • 23
1

With the help of this post.

You can parse your string using JSON.parse() (if you don't already have it as an object), and then recursively run through all its properties. For properties that are neither an object nor an array, encode each value using encodeURIComponent().

DEMO:

Here's the JSFiddle.

CODE:

Consider obj a parsed object from your first JSON string in your question. Here's the code in case the JSFiddle link breaks:

escapeObject(obj);
var escaped = JSON.stringify(obj);
document.getElementById("result").innerHTML = escaped;

function escapeObject(theObject) {
  var result = null;
  if(theObject instanceof Array) {
    for(var i = 0; i < theObject.length; i++) {
        result = escapeObject(theObject[i]);
        if (result) {
            break;
        }   
    }
  }
  else
  {        
      for(var prop in theObject) {    
        if (typeof theObject[prop] == typeof "") {
          var escValue = encodeURIComponent(theObject[prop]);
          theObject[prop] = escValue;  
        }
        if(theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
            result = escapeObject(theObject[prop]);
            if (result) {
                break;
            }
        }

    };
  }
  return result;
}
Community
  • 1
  • 1
guysigner
  • 2,822
  • 1
  • 19
  • 23