News for hackers. Stuff that matters.
Re:The Pretty JSON Revolution by cratermoon on Tuesday February 23, 2021 @12:25PM (#26240007)
I recall seeing something that claimed all JSON is syntactically valid JavaScript. If that's correct, shouldn't it be possible to use JS code formatting engines to intelligently format JSON?
Re:The Pretty JSON Revolution by sefrost on Tuesday February 23, 2021 @12:29PM (#26240071)
Yes, Prettier can format JSON.

https://prettier.io/docs/en

Re:The Pretty JSON Revolution by patdx on Wednesday February 24, 2021 @02:23AM (#26247527)
Basically yes, though `{` and `}` also start and end expression blocks in JavaScript. So the JS formatter needs to be aware that it is working with a JSON object and not a complete expression.

Valid JSON:

  {
    "key1": "hello",
    "key2": "world"
  }
You could "trick" a JS formatter to format it by wrapping with a fake function, etc. Some minimum valid JS:

  json({
    key1: "hello",
    key2: "world",
  });
MongoDB has some JS libraries that use similar tricks to use JS parsers for their shell query format (which is similar to JSON). For example, around line 597: https://unpkg.com/browse/[email protected]/dist/ejson...
Re:The Pretty JSON Revolution by binarymax on Tuesday February 23, 2021 @12:41PM (#26240229)
Easy node one-liner:

JSON.stringify(JSON.parse(require('fs').readfileSync('myfile.json')),null,2);