-2

I've a function that print out this format:

{
"name": "Google",
"url": "https:/google.com"
}
,
{
"name": "Facebook",
"url": "https://wts-kalix.polystar.net/Kalix/#/portal/619551b520fbf40c1cf22bad/618419244a4be06500ba0e8a"
}
,

And i need to delete the last comma so that the JSON format become valid which character should i use when it prints out

  • https://stackoverflow.com/questions/952924/how-do-i-chop-slice-trim-off-last-character-in-string-using-javascript – Emil Karlsson Nov 30 '21 at 08:58
  • 2
    Don't construct JSON's yourself. Make an array of objects and use `JSON.stringify()` for that. Too often things go wrong when you do it manually. (And then we get other questions here on Stack Overflow asking how to fix malformed JSON, which is near impossible.) – Ivar Nov 30 '21 at 08:59
  • 1
    Is this your function or some third-party code you don't control? It feels weird to have a function that generates JSON yet produces slightly invalid output. Generating valid JSON at once is much easier than breaking it and repairing it afterwards. – Álvaro González Nov 30 '21 at 09:00

1 Answers1

0

If your input to your formatter is an array, you can just use Array.join, it does that automatically. You can in theory most likely also use JSON.stringify

GoldenretriverYT
  • 3,043
  • 1
  • 9
  • 22