# Make \`JSON.stringify\` more convenient?

**URL:** <https://es.discourse.group/t/make-json-stringify-more-convenient/745>\
**Category:** 💡 Ideas\
**Created:** [April 19, 2021, 10:10am UTC](https://es.discourse.group/t/make-json-stringify-more-convenient/745 "2021-04-19T10:10:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aleen42](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aleen42/32/461_2.png) [@aleen42](https://es.discourse.group/u/aleen42)\
**Post date:** [April 19, 2021, 10:10am UTC](https://es.discourse.group/t/make-json-stringify-more-convenient/745/1 "2021-04-19T10:10:26Z")

</div>

As we all know, `JSON.stringify` has accepted the second parameters as `replacer`, while the third one as `space`. Recently I found that it is not convenient for me to add spaces for only certain levels of an object during stringifying like:

Converting this

```javascript
{
    a : 1,
    b : {
        c : 1,
    }
}

```

To

```json
{
    "a": 1,
    "b": {"c":1}
}

```

It may be more convenient for the third parameters `space` to be defined as a handler function like `replacer`:

```javascript
JSON.stringify({
    a : 1,
    b : {
        c : 1,
    }
}, null, key => key === 'b' ? 0 : 4); 

```

---

<div class="post-metadata">

**Author:** ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)\
**Post date:** [April 19, 2021, 11:09pm UTC](https://es.discourse.group/t/make-json-stringify-more-convenient/745/2 "2021-04-19T23:09:50Z")

</div>

I don't think this is a sufficiently common thing for people to care about that it warrants support in the language.

---

<div class="post-metadata">

**Author:** ![aleen42](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aleen42/32/461_2.png) [@aleen42](https://es.discourse.group/u/aleen42)\
**Post date:** [April 20, 2021, 1:21am UTC](https://es.discourse.group/t/make-json-stringify-more-convenient/745/3 "2021-04-20T01:21:23Z")

</div>

It's helpful for constructing JavaScript files.

Current version:

```javascript
var obj = {
	a : 1,
	b : {
		c : 1,
	}
};

fs.writeFileSync('test.js', `
module.exports = ${JSON.stringify(obj, (k, v) => k === 'b' ? JSON.stringify(v, null, 0) : v, 4)
	.replace(/\\"/g, '"')
	.replace(/"{/g, '{')
	.replace(/}"/g, '}')};
`, {encoding: 'utf8'});

```

After that proposal:

```javascript
fs.writeFileSync('test.js', `
module.exports = ${JSON.stringify(obj, null, key => key === 'b' ? 0 : 4)};
`, {encoding: 'utf8'});

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [April 20, 2021, 1:52am UTC](https://es.discourse.group/t/make-json-stringify-more-convenient/745/4 "2021-04-20T01:52:54Z")

</div>

That seems like something you'd want to use an actual parser on - eslint, babel, something like that.

---

<div class="post-metadata">

**Author:** ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)\
**Post date:** [April 20, 2021, 1:59am UTC](https://es.discourse.group/t/make-json-stringify-more-convenient/745/5 "2021-04-20T01:59:51Z")

</div>

I would strongly advise the use of a tool like prettier for pretty-printing JavaScript files, and that you construct them using estree or shift-ast or something rather than mashing strings together. JSON.stringify is very much not intended for that purpose and I do not think the committee will be receptive to feature suggestions aimed at that use case.

---

<div class="post-metadata">

**Author:** ![aleen42](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aleen42/32/461_2.png) [@aleen42](https://es.discourse.group/u/aleen42)\
**Post date:** [April 20, 2021, 2:10am UTC](https://es.discourse.group/t/make-json-stringify-more-convenient/745/6 "2021-04-20T02:10:44Z")

</div>

I know there are few libraries for modifying JavaScript files directly, and developers always like to use JSON files to store objects because it is easily serialized. However, JSON files sometimes have their disadvantages like unable to comment, dynamic export, etc. It is just a suggestion since that `JSON.stringify` has provided `space` for prettifying.

---

<div class="post-metadata">

**Author:** ![tracker1](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tracker1/32/722_2.png) [@tracker1](https://es.discourse.group/u/tracker1)\
**Post date:** [April 21, 2021, 2:11am UTC](https://es.discourse.group/t/make-json-stringify-more-convenient/745/7 "2021-04-21T02:11:58Z")

</div>

This kind of logic in the language will create a lot of additional overhead for very little value. If you wish to format your JSON, there are many different libraries that allow for this, and you can include/use them.

edit: You may also wish to look into YAML or TOML, both of which imho are slightly more readable. Also, to my knowledge a lot of formatters/libraries won't preserve comments, not sure about JSON5.
