1

Is there a way to turn {"query":{"match_all" : {} }} into {\n "query":{\n "match_all" : {}\n }\n}?

I have tried with

const stringify = require("json-stringify-pretty-compact");

const a = '{\n  "query":{\n    "match_all" : {}\n  }\n}';
const b = '{"query":{"match_all" : {} }}';
console.log(a);
console.log(b);
const c = stringify(b);
console.log(c);

where I get

{
  "query":{
    "match_all" : {}
  }
}


{"query":{"match_all" : {} }}
"{\"query\":{\"match_all\" : {} }}"

For some reason the \n's are missing.

Sandra Schlichting
  • 25,050
  • 33
  • 110
  • 162
  • 1
    `stringify`!=`prettify`. You should pass an object to it, not a JSON text. – Bergi Jun 22 '21 at 10:33
  • 1
    This is a little unclear. Do you want the output to contain the literal characters `\n`? – Simon Brahan Jun 22 '21 at 10:34
  • 1
    Do you want to pretty existing JSON data (i.e. text) or a JavaScript value? If the latter then it's a duplicate of [pretty-print JSON using JavaScript](https://stackoverflow.com/q/4810841/218196) – Felix Kling Jun 22 '21 at 10:34
  • @SimonBrahan Yes, I would like to have the `\n`'s and white spaces inserted, so the variable becomes `{\n "query":{\n "match_all" : {}\n }\n}` – Sandra Schlichting Jun 22 '21 at 10:55
  • @Bergi The reason I gave https://www.npmjs.com/package/json-stringify-pretty-compact a try was because it looked like the purpose of the package were to prettify the string. – Sandra Schlichting Jun 22 '21 at 10:59
  • @FelixKling Yes, but the outout should have `\n` and white spaces inserted, so it becomes `{\n "query":{\n "match_all" : {}\n }\n}`. – Sandra Schlichting Jun 22 '21 at 11:00
  • 2
    @SandraSchlichting No. The docs state "*it’s like `JSON.stringify`*". With prettier output than the native method, yes, but still the same interface - you need to pass data not JSON strings. – Bergi Jun 22 '21 at 11:21
  • 1
    It's still not quite clear to me what your actual input is. If it is JSON (i.e. text) you should be able to use https://prettier.io/ to prettify it. – Felix Kling Jun 22 '21 at 12:00

2 Answers2

3

Whoops, it looks like you need to parse that JSON first.

Try this:

const stringify = require("json-stringify-pretty-compact");

const a = '{\n  "query":{\n    "match_all" : {}\n  }\n}';
const b = '{"query":{"match_all" : {} }}';
console.log(a);
console.log(b);
const c = stringify(JSON.parse(b));
console.log(c);

You can use JSON.stringify instead – Just set the third argument to a number (to indent by that many spaces) or a string (to indent by that string).

Just set the second argument to null for now

Example:

const a = '{\n  "query":{\n    "match_all" : {}\n  }\n}';
const b = '{"query":{"match_all":{}}}';
console.log(a);
console.log(b);
const c = JSON.stringify(JSON.parse(b), null, 4);
console.log(c);

Another option, if you are just debugging, would be to use require('util').inspect(obj), which console.logs the object. But the advantage of this method is that you can choose between a compact format or the normal format, you can apply syntax highlighting, and you can even choose the depth of the object to be logged. There's more info here and here, but I think you could use require('util').inspect(obj, { depth: Infinity, colors: true, compact: false })

0

Have you tried this ?

JSON.stringify(b, null, 2) // 2 for 2 whitespace indent
.replace(/\n/g, '\\n') // To display \n rather than an actual line return

const obj = {"query":{"match_all" : {} }}

console.log(JSON.stringify(obj, null,2).replace(/\n/g, '\\n'))
gladix
  • 302
  • 2
  • 11
  • I think `\n`s in OP's example meant to be *real* linefeeds, otherwise you're right – FZs Jun 22 '21 at 15:58