0

I have a json string,

{"eID":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX","cID":"XXXXXXXXXX-XXXXC-XXXX-XXXX-XXXXXXX"}. 

I obtained this by stringifying the JSON. But I need the JSON file in the format in which each keys and values have the escape character, like

{\"eID\":\"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX\",\"cID\":\"XXXXXXXXXX-XXXXC-XXXX-XXXX-XXXXXXX\"}. 

How can I achieve this in JavaScript?

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
Johncy Binoy
  • 169
  • 3
  • 13

3 Answers3

4

You could stringify again.

var string = '{"eID":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX","cID":"XXXXXXXXXX-XXXXC-XXXX-XXXX-XXXXXXX"}';

console.log(JSON.stringify(string));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

If you want to replace " with \" then simply replace(/"/g, "\\\"") on the stringified value.

var obj = {
  "eID": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX",
  "cID": "XXXXXXXXXX-XXXXC-XXXX-XXXX-XXXXXXX"
};

console.log(JSON.stringify(obj).replace(/"/g, "\\\""));

If want to revert this replace change back to JSON value, then use replace(/\\\"/g, "\"")

Demo

var obj = {"eID":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX","cID":"XXXXXXXX‌​XX-XXXXC-XXXX-XXXX-X‌​XXXXXX", "user": "domain\\username" };
var escapedValue = JSON.stringify( obj ).replace(/"/g, "\\\"")
var oldValue = escapedValue.replace(/\\\"/g, "\"");
console.log( oldValue );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • It seems pretty clear that what OP actually needs (though they may not know it) is to double-JSON-encode the value. If so, string replacement is a poor way to go about that. – JLRishe Nov 07 '17 at 09:17
  • @JLRishe Output from my code matches the OP's required output exactly. Not sure what you meant by *though they may not know it*. And what do you mean by *regex is a poor way to go about that* ? – gurvinder372 Nov 07 '17 at 09:18
  • _"Output from my code matches the OP's required output exactly"_ Yes, it works for this very limited example. I think you're missing the bigger picture. _"Not sure what you meant by though they may not know it."_ I mean that OP is asking an [XY Question](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) where X is how to JSON-escape a JSON string, and Y is "how do I escape these quotation marks?" _"regex is a poor way to go about that?"_ There are tried and true ways to JSON-escape values. Using string replacement to do it manually is hacky and prone to bugs. – JLRishe Nov 07 '17 at 09:33
  • @JLRishe Big picture is not relevant here since this is a very simply and limited question by OP. Unless there is a follow up question by OP - **this works but...can you also suggest..?**, there is no way to tell if this is an XY problem. And please justify your statement - *Using string replacement to do it manually is hacky and prone to bugs* in the context of this question. – gurvinder372 Nov 07 '17 at 09:37
  • The context of this question is that OP wants to JSON-encode a JSON value. There's a method for JSON-encoding values, and it's not `.replace()`. Recommending anything inferior to `JSON.stringify()` is bad advice. It doesn't matter whether it works in this very limited example. `JSON.stringify()` works in this example _and_ it's the better choice. – JLRishe Nov 07 '17 at 09:42
  • @JLRishe There is a difference between a *inferior choice*, *poor choice* and *wrong choice*. And this is definitely not a *wrong choice*. You have failed to state how this is a *poor choice*. How would my code be *hacky and prone to bugs*? There could be multiple ways to solve one coding problem, there are numerous such examples all over SO such as https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript . And **OP is not even asking for most efficient way** of solving his problem at first place. – gurvinder372 Nov 07 '17 at 09:51
  • Let's imagine that a month from now, OP's situation changes and the object being passed through their progam now contains values with backslashes: `{"eID":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX", "user": "domain\\username" }` Maybe this even takes place without their knowledge. Your code only handles quotation marks, so now their code, which was seemingly working, is now broken and they have to figure out why or come back here and ask. Or, you could recommend them the best approach from the start, and not risk their code breaking due to perfectly foreseeable circumstances. Your choice. – JLRishe Nov 07 '17 at 10:02
  • @JLRishe FWIW, my code works for your example as well. If it helps, I can update my answer to demonstrate this as well. – gurvinder372 Nov 07 '17 at 10:04
  • It doesn't work. It wouldn't throw an error during escaping, but if someone took the output and parsed it, then parsed it again (since it's supposed to be double-escaped), the second parse would fail: https://jsfiddle.net/nwn1gqbu/ – JLRishe Nov 07 '17 at 10:09
  • @JLRishe There seems to be a confusion. OP is not asking for *double stringification*, only **I need the JSON file in the format in which each keys and values have the escape character** - in OP's words. If there was a mention of parsing the escaped value, my answer definitely would be a **wrong choice**, forget about being inferior. – gurvinder372 Nov 07 '17 at 10:13
  • And why do you think OP is asking how to change `"` to `\"` if the goal is not to escape the value? – JLRishe Nov 07 '17 at 10:18
  • @JLRishe Escaping is understood and handled, I am trying to understand from where did you get this idea of *the second parse would fail*? When did OP asked for using `JSON.parse` on *escaped value* as an output of expected answer? Only *double quotes* needs to be escaped which can be reverted back before using `JSON.parse`. – gurvinder372 Nov 07 '17 at 10:22
  • OP is asking for a way to take a JSON string (which would already be JSON-encoded) and escape it _again_. Meaning that it would be double-encoded JSON. It stands to reason, then, that the result, at some later point, would be parsed once to obtain the encoded JSON, and then parsed again (perhaps even later) to deserialize the JSON. Double encoding -> double parsing. – JLRishe Nov 07 '17 at 10:33
  • @JLRishe *Meaning that it would be double-encoded JSON*, **No** they don't mean the same thing. And yes, if output from `replace(/"/g, "\\\"")` has to be parsed back, you can always **un-replace** it by doing `replace(/\\\"/g, "\"")` first rather than doing an expensive `JSON.parse`. – gurvinder372 Nov 07 '17 at 10:38
  • _"And yes, if output from replace(/"/g, "\\\"") has to be parsed back, you can always un-replace it by doing replace(/\\\"/g, "\"") first rather than doing an expensive JSON.parse."_ You're assuming that OP has control over how the output is handled. By stating that they have to the output in a particular format, this that they _don't_ have control over how it's handled after that point, so no, I wouldn't assume that `replace(/\\\"/g, "\"")` is an option. – JLRishe Nov 07 '17 at 10:42
  • @JLRishe except I haven't assumed anything, and you are the one who has assumed *why* OP has asked for it, *what* OP would like to with it, and whether OP has any control over it or not. OP simply asked for an escaping the double quotes, please re-read the question again. – gurvinder372 Nov 07 '17 at 10:44
  • Yes, and I assert that they are asking an [XY question](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) Why else would someone want to change the quotes in their JSON to `\"`? It would be a nonsense request. Anyway, I've said all I have to say. Good day. – JLRishe Nov 07 '17 at 10:47
-1

You can use "JSON.stringify(your_json)" to parse your json to plain text.

var json = {"eID":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX","cID":"XXXXXXXXXX-XXXXC-XXXX-XXXX-XXXXXXX"};


document.getElementById('result').innerText = JSON.stringify(json);
<span id="result"></span>
victor_reiner
  • 107
  • 1
  • 8