Having a valid JS Object (ES6 formatted with trailing comma) in a string in for example a browser or node, how to get a valid JSON-object out of this? https://www.convertsimple.com/convert-javascript-to-json/ does it for example.
"Of course" I know about JSON.parse and JSON.stringify but not sure it can do the trick in this case. :) (Or at least, I don't see how). I also would like to avoid eval.
Example:
const jsObjectInString = `{
key: "value",
test: 1,
result: [
{
subKeyOne: "test"
},
{
subKeyTwo: "test value with space",
test: 2
}
],
}`;
Expected result:
{
"key": "value",
"test": 1,
"result": [
{
"subKeyOne": "test"
},
{
"subKeyTwo": "test value with space",
"test": 2
}
]
}