0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JSON5でJavaScriptオブジェクトのstringify

Posted at

標準のJSON.stringifyは欲しいフォーマットで簡単に出力できなかったので、JSON5を使って見ました。

JSON5とは

github

The JSON5 Data Interchange Format (JSON5) is a superset of JSON that aims to alleviate some of the limitations of JSON by expanding its syntax to include some productions from ECMAScript 5.1.

欲しいフォーマット

const obj = {a: 123, b: 'hello'};
console.log(JSON.stringify(obj, null, '  '));
  • 複数行で記載する場合、後ろにコンマが付く
  • キーが文字列なので、クオテーションで括らない
{
  a: 123,
  b: 'hello',
}

JSON.stringifyを使う場合

{
  "a": 123,
  "b": "hello",
}

JSON5.stringifyを使う場合

{
  a: 123,
  b: 'hello',
}

JSON5で流石に人間に優しくフォーマットしてくれますね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?