how to strip json comments in javascript

How to strip json comments in javascript ? I think a regex will be a concise way.

{
    "filed": { // comments
         "n": 1 /* multi-line ...
                                 comments */
    },
    "field2": " // comments in string , /* here */ "
}

should work on the above example.
Note: the comments in the string should not be removed.

Update: i know that comments in json are not valid, but for simplicity i want to have them and then remove them, i see a lot of tools support parsing jsons which have comments.

Update 2: the above example itself is a string, i didn't explain that (sorry), and now see some answers thinking its a js code.

Update 3: i again forgot to put keynames in double quotes, updated the question.


Solution 1:

While commenting in JSON is not a standard practice, in some cases it could be useful. For this purpose I wrote small package for nodejs: json-easy-strip

Here is it's main source code:

data

{
    /*
     * Sweet section
     */
    "fruit": "Watermelon", // Yes, watermelons is sweet!
    "dessert": /* Yummy! */ "Cheesecake",
    // And finally
    "drink": "Milkshake - /* strawberry */ or // chocolate!" // Mmm...
}

one-liner parser

data = data.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m);

result

console.log(data);

//  {
//    fruit: 'Watermelon',
//    dessert: 'Cheesecake',
//    drink: 'Milkshake - /* strawberry */ or // chocolate!'
//  }

Here is demo

Solution 2:

One simple way is to run JSON.stringify and then JSON.parse and voila it's clean.

Solution 3:

Let the javascript parser do it:

a = {
    filed: { // comments
         n: 1 /* multi-line ...
                                 comments */
    },
    field2: " // comments in string , /* here */ "
}
a.toSource()
/*
({filed:{n:1}, field2:" // comments in string , /* here */ "})
*/

You could do it with a string (according to the edited question) the very same way:

a =   'a = {\r\n  filed: { // comments  n: 1 \r\n/* multi-line ...  comments */\r\n},\r\n'
    + 'field2: " // comments in string , /* here */ "\r\n}'
eval(a).toSource().toString();

But it doesn't seem to work with chrome? Oh, forgot, it is FF only, sorry (in Javascript version 3).

I found a replacement at https://gist.github.com/archan937/1961799 so the following works in most of the rest of the browsers, too.

function inspect(object) {
  switch (typeof(object)) {
  case "undefined":
    return "undefined";
  case "string":
    return "\"" + object.replace(/\n/g, "\\n").replace(/\"/g, "\\\"") + "\"";
  case "object":
    if (object == null) {
      return "null";
    }
    var a = [];
    if (object instanceof Array) {
      for (var i in object) {
        a.push(inspect(object[i]));
      };
      return "[" + a.join(", ") + "]";
    } else {
      for (var key in object) {
        if (object.hasOwnProperty(key)) {
          a.push(key + ": " + inspect(object[key]));
        }
      };
      return "{" + a.join(", ") + "}";
    }
  default:
    return object.toString();
  }
};

a = 'a = {\r\n  filed: { // comments  n: 1 \r\n/* multi-line ...  comments */\r\n},\r\n'
    + 'field2: " // comments in string , /* here */ "\r\n}'

alert(inspect(eval(a)));

Solution 4:

Just a personal opinion, if you want to keep an invalid JSON full of comments for production purposes, I suppose its up to you, but deploying that stuff to your live website and then filling it with JavaScript hacks to make it work doesn't look good at all. I suggest you generate a clean JSON file for deployment, and keep the commented code to yourself. The end user doesn't have to see that, and he certainly doesn't need a bunch of indenting spaces increasing amount of data transfered aswell.

In any case, here is one way of removing those comments:

var invalid_json = "{\n\"filed\": { // comments\n\"n\": 1 /* multi-line ...\ncomments */\n},\n\"field2\": \" // comments in string , /* here */ \"\n}";

eval("var x = " + invalid_json);
var x = JSON.stringify(x);
alert(x); // x should be valid JSON of that

Another way is to use it as JavaScript instead of JSON, you will just have to modify the file a bit:

// file.js
var data = {
    "filed": { // comments
         "n": 1 /* multi-line ...
                                 comments */
    },
    "field2": " // comments in string , /* here */ "
};

In the page:

<script type="text/javascript" src="file.js"></script>

This should import the object to your page with the name data.

Solution 5:

If you need to remove comments, with support for JSON5, have a look at library decomment.

It can remove comments from JSON5, JavaScript ES6, CSS and HTML, in the same simple way.

var decomment = require('decomment');

var code = "var t; // comments";

decomment(code); //=> var t;