DEV Community

artydev
artydev

Posted on

JSON5 : JSON with superpower

Have you ever wanted to use multilines strings in JSON objects ?
JSON5 allows that and much more

You can test it here : JSON5-TEST

<script src="https://unpkg.com/json5@^2.0.0/dist/index.min.js"></script>

<script>
  const mscripts = { 
    hello : `
      function hello() {
        alert('hello from JSON5')
      };
      hello();
      `
  }
  let str = JSON5.stringify(mscripts) 
  let res  = JSON5.parse(str) 
 eval(mscripts.hello) 
</script>
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
leob profile image
leob

Hm, not sure if the tiny number of "advantages" of JSON5 justify breaking compatibility with a gazillion existing software programs and components which expect the 'classical' JSON standard. Only for a very narrow set of use cases maybe.

Collapse
 
artydev profile image
artydev

Hy,

Thanks for your post.
I don't think the purpose is to replace standard JSON.
It's another tool in your toolbox

Regards

Collapse
 
leob profile image
leob

For sure :-)