Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate JSON5 #122

Open
stevage opened this issue Jun 14, 2016 · 7 comments
Open

Generate JSON5 #122

stevage opened this issue Jun 14, 2016 · 7 comments
Assignees

Comments

@stevage
Copy link

stevage commented Jun 14, 2016

If you're looking for features to add to cli.js, it would be useful to automatically generate JSON5 from JSON, with a few flavour flags. Stripping quotes from keys where not needed is the obvious one. (HJSON has a feature like this, but unfortunately the output is not compatible with JSON5).

@jordanbtucker jordanbtucker self-assigned this Sep 20, 2017
@jordanbtucker jordanbtucker modified the milestone: v1.0.0 Sep 20, 2017
@Kikketer
Copy link

Kikketer commented Apr 9, 2021

This may not work but for me as a workaround I typically put the JSON in something as simple as the debugger tools on the browser. Then JSON.parse() that into a variable and then save it.

@emilebosch
Copy link

emilebosch commented Aug 11, 2021

🤔 What the status on this? I literally can't find a single library that converts json to json5, even though I would love to adopt this. Or am I looking it wrong?

@jordanbtucker
Copy link
Member

@emilebosch This library can convert JSON to JSON5. Here's a snippet of code to do this:

const JSON5 = require('json5')

const json = `{ "name": "Jane", "age": 25, "married": true }` // Or read JSON from a file into a variable.
const value = JSON.parse(json)

const json5Compact      = JSON5.stringify(value)
const json5Indented     = JSON5.stringify(value, null, 2)
const json5DoubleQuoted = JSON5.stringify(value, {space: 2, quote: '"'})

console.log(json5Compact)
console.log(json5Indented)
console.log(json5DoubleQuoted)
{name:'Jane',age:25,married:true}
{
  name: 'Jane',
  age: 25,
  married: true,
}
{
  name: "Jane",
  age: 25,
  married: true,
}

If you're looking for a CLI that does this, then this library does not have that feature.

@emilebosch
Copy link

emilebosch commented Aug 11, 2021

Ah Jordan, I meant a cli indeed, sorry about that :) I do a lot of JSON processing and having it in json5 would be so easy on the eyes. Do you have anything against having a cli flag or can I just submit a PR? I was thinking of accepting stdin and a file.

@jordanbtucker
Copy link
Member

jordanbtucker commented Aug 11, 2021

PRs are welcome! Please make it backward compatible with the current CLI, so that it can just be a minor release.

Note that the -c, --convert option is not documented, but it exists for backward compatibility with older versions of the CLI.

@emilebosch
Copy link

Sure. Struggling with the name of the option though. Got -e for emit now. I.e emit JSON5. Can also do -r or --reverse?

json5 --emit <stdin|file>

@jordanbtucker
Copy link
Member

jordanbtucker commented Aug 11, 2021

-f, --format specifying json or json5 (case insensitive, default json), maybe? We'd probably also want to add -q, --quote since we have --space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants