0

Is somethhing like this possible in json

let a = {
    "awe
     sdsds" : "eweq
               dfs
               ewewew"
}

can i have multiline strings as key and value

I tried and got this icon at newline for key and \n this at newline for value

  • you could use yaml and convert it to json. otherwise (i didn’t test it) you may try to escape a line break with a backslash – Psi Jul 22 '22 at 18:27
  • 1
    No, it's not possible: control characters (linefeed in particular) are not permitted in JSON strings (https://www.json.org) – Blackhole Jul 22 '22 at 18:33
  • Does this answer your question? [Can a JSON value contain a multiline string](https://stackoverflow.com/questions/16690101/can-a-json-value-contain-a-multiline-string) – Sean Sutherland Jul 22 '22 at 18:39
  • 1
    This is not "JSON", it is object literal. Do you really have a JSON though? Also looks like https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Yury Tarabanko Jul 22 '22 at 18:40

1 Answers1

2

This is not "JSON", it is object literal.

If you are having a literal you can use string literals and computed properties if you really want to have this kind of keys. Otherwise you'd need to use escape seq \n (see the result of JSON.stringify)

let a = {
    [`awe
     sdsds`] : `eweq
               dfs
               ewewew`
}

console.log(a[`awe
     sdsds`])

console.log(JSON.stringify(a))
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98