Reputation: 1080
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.
Upvotes: 14
Views: 13641
Reputation: 610
I see a lot of answers using eval
. While these methods work, using eval
can be risky. Though these ways certainly pose no threat, I advocate to never use evel
, even for simple things like this.
Here's my adaptation of Havenard's answer that uses a similar, but different method.
var invalid_json = "{\n\"filed\": { // comments\n\"n\": 1 /* multi-line ...\ncomments */\n},\n\"field2\": \" // comments in string , /* here */ \"\n}";
var json = (new Function("return " + invalid_json))();
var x = JSON.stringify(json);
alert(x); // x should be valid JSON of that
Upvotes: 1
Reputation: 2549
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
Your JSON file could contain any valid JS comments:
{
/*
* Sweet section
*/
"fruit": "Watermelon", // Yes, watermelons is sweet!
"dessert": /* Yummy! */ "Cheesecake",
// And finally
"drink": "Milkshake - /* strawberry */ or // chocolate!" // Mmm...
}
And here is how we strip it:
//
// Our JSON string
let jsonString = `{
/*
* Sweet section
*/
"fruit": "Watermelon", // Yes, watermelons is sweet!
"dessert": /* Yummy! */ "Cheesecake",
// And finally
"drink": "Milkshake - /* strawberry */ or // chocolate!" // Mmm...
}`;
//
// One-liner comment stripper
jsonString = jsonString.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m);
//
// Parse jsonString and print result
console.log(JSON.parse(jsonString));
//
// Result
// {
// fruit: "Watermelon",
// dessert: "Cheesecake",
// drink: "Milkshake - /* strawberry */ or // chocolate!"
// }
Upvotes: 16
Reputation: 25840
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;
Upvotes: 2
Reputation: 5525
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)));
Upvotes: 3
Reputation: 27864
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
.
Upvotes: 1
Reputation: 1649
One simple way is to run JSON.stringify
and then JSON.parse
and voila it's clean.
Upvotes: 4