0

I have an input JSON like this (which really contains the literal values "\u2013" (the encoded form of a unicode character)):

{"source":"Subject: NEED: 11/5 BNA-MSL \u2013 1200L Departure - 1 Pax"}

I read it with JSON.parse and it reads the \u2013 as , which is fine for display in my app.

However, I need to export again the same JSON, to send it down to some other app. I want to keep the same format and have back the \u2013 into the JSON. I am doing JSON.stringify, but it keeps the in the output.

Any idea what I could do to keep the \u syntax?

starball
  • 20,030
  • 7
  • 43
  • 238
Baptiste Pernet
  • 3,318
  • 22
  • 47
  • Note: while the asker has indicated that they found an answer to their question in georg's answer (31652607) to [this question (31649362)](https://stackoverflow.com/q/31649362/11107541), the questions are not the same, so please do not mark it as a duplicate of it. The asker over there actually clarified that the answer that is wanted here does not work there. Migrating the answer could be appropriate (not sure how to work out logistics though). – starball Dec 17 '22 at 04:49

1 Answers1

1

Using a replacer function in a JSON.stringify call didn't work - strings returned from the replacer with an escaped backslash produce a double backslash in output, and a single backslashed character is unescaped in output if possible.

Simply re-escaping the stringify result has potential:

const obj = {"source":"Subject: NEED: 11/5 BNA-MSL \u2013 1200L Departure - 1 Pax"}
console.log("  stringify:  ", JSON.stringify( obj));
console.log("& replaceAll: ", JSON.stringify(obj).replaceAll('\u2013', '\\u2013'));

using more complex string modifications as necessary.

However this looks very like an X solution to an X-Y problem. Better might be to fix the downstream parsing to handle JSON text as JSON text and not try to use it in raw form - particularly given that JSON text in encoded in utf-8 and can handle non-ASCII characters without special treatment.

traktor
  • 17,588
  • 4
  • 32
  • 53
  • 2
    Thank you for your answer. I also found that https://stackoverflow.com/questions/31649362/json-stringify-and-unicode-characters that works well in my case. Also I can't fix downstream unless you know someone at AWS that will fix my issues :) – Baptiste Pernet Dec 13 '22 at 00:06
  • I can fix your downstream issues. What are they? – Wyck Dec 19 '22 at 20:10
  • @Wyck we read a JSONL file taken from AWS sage maker ground truth labelling job, to then feed it into a comprehend NER training. AWS sage maker puts all the json with the `\u` notation and given how the labelling is done, it's important to keep the same representation. It's not really a fix maybe, I think it's just how it expects the input. – Baptiste Pernet Jan 24 '23 at 22:46