0

I am using Javascript's JSON.stringify() function to convert a list of strings into a comma separated JSON object.

array = ["a", "b", "c"];
console.log(JSON.stringify(array));
// Output: ["a","b","c"]

I want to add a single space between list elements. I attempted to use the space parameter, but this added extra newlines to my output.

array = ["a", "b", "c"];
console.log(JSON.stringify(array, null, " "));

// Desired output:
// ["a", "b", "c"]
// 
// Actual output:
// [
//  "a",
//  "b",
//  "c"
// ]

There is a similar question about adding spaces between objects with JSON.stringify. However, the top answer recommends splitting elements by curly brackets and rejoining, whereas list elements are not separated by curly brackets.

How can I output JSON list elements separated by with a space when using JSON.stringify?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
  • 1
    Can you replace each comma with a comma and a space? – mykaf Jan 17 '23 at 16:42
  • @mykaf Good idea, using `JSON.stringify(array).replaceAll(',', ', ')` seems to work for my specific case since the list is my only JSON element. Feel free to post as an answer. – Stevoisiak Jan 17 '23 at 16:44
  • `console.log(JSON.stringify(array, null, '\t'));` idk if this might help but is worth a try – Chris G Jan 17 '23 at 16:47
  • @ChrisG Using `\t` adds newlines and tabs between the list elements. That is useful, but not exactly what I'm looking for in this case. – Stevoisiak Jan 17 '23 at 16:49
  • 3
    Why do you need to do this? JSON is meant for communication between computers, not for human display. – Barmar Jan 17 '23 at 16:57
  • @Barmar I am working with a script that auto-generates Python code. Conveniently, the output for `JSON.stringify` matches the format of a Python list. I want to add spaces so the generated code matches the company's code style guidelines. I acknowledge this is an unusual use case. – Stevoisiak Jan 17 '23 at 17:43
  • @Stevoisiak, please have added those `company's code style guidelines` to the question. It looks ambiguous and inconsistent as it's written now. – Kosh Jan 17 '23 at 17:46
  • @Kosh I'm confused why the question would be ambiguous. I had hoped the provided example of the desired output would clear up any ambiguity. – Stevoisiak Jan 17 '23 at 17:59
  • `["a", "b", "c"]` doesn't look like real life input. And if we're talking of `guidelines` please add them. – Kosh Jan 17 '23 at 18:08
  • @Kosh I am working with a list of plaintext strings, so it fairly closely mirrors my real world input values. I've edited my question to specify the elements are strings. – Stevoisiak Jan 17 '23 at 18:12
  • 1
    Then you might not need `JSON.stringify` just `\`["${array.join('", "')}"]\``. – Kosh Jan 17 '23 at 19:39
  • @Kosh Using `.join()` worked perfectly, I was overthinking the issue. Can you post that as an answer? – Stevoisiak Jan 17 '23 at 19:58
  • IMHO, with all due respect, nor the question neither my suggestion are not really valuable to be posted here. Just yet another specific case. – Kosh Jan 17 '23 at 20:23

2 Answers2

0

Similar to @mykaf's answer, but will not change string contents containing commas due to character escaping.

const array = ["string without commas", "string,with,commas"];
const myJSON = JSON.stringify(array).replaceAll(',"', ', "');
// '["string without commas", "string,with,commas"]'

Just know that this only matters while your array is stringified. As soon as you deserialize back to an object, there are no commas in between elements, as that concept exists only in strings, not JSON objects/arrays.

Jacob Stamm
  • 1,660
  • 1
  • 29
  • 53
  • 1
    As an extra sanity check, you could match both quotation marks. ie: `JSON.stringify(array).replaceAll('","', '", "');` – Stevoisiak Jan 17 '23 at 18:20
-3

Since list items are separated by a comma, it seems that replacing commas with a comma and a space should do the trick:

var myJSON = JSON.stringify(array).replaceAll(',', ', ');

mykaf
  • 1,034
  • 1
  • 9
  • 12
  • 4
    This changes the contents of string array elements which contain commas. `string with,comma` -> `string with, comma` – Jacob Stamm Jan 17 '23 at 16:55