137

I am writing a JSON file which would be read by a Java program. The fragment is as follows...

{
  "testCases" :
  {
    "case.1" :
    {
      "scenario" : "this the case 1.",
      "result" : "this is a very long line which is not easily readble.
                  so i would like to write it in multiple lines.
                  but, i do NOT require any new lines in the output.
                  I need to split the string value in this input file only.
                  such that I don't require to slide the horizontal scroll again and again while verifying the correctness of the statements.
                  the prev line, I have shown, without splitting just to give a feel of my problem"
    }
  }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user2409155
  • 1,379
  • 2
  • 8
  • 3
  • { "testCases" : { "case.1" : { "scenario" : "this the case 1.", "result" : "this is a very long line which is not easily readble. so i would like to write it in multiple lines. but, i do NOT require any new lines in the output. I need to split the string value in this input file only. such that I don't require to slide the horizontal scroll again and again while verifying the correctness of the statements. the prev line, I have shown, without splitting just to give a feel of my problem" } } } } – user2409155 May 22 '13 at 11:04
  • 4
    possible duplicate of [Multiline strings in JSON](http://stackoverflow.com/questions/2392766/multiline-strings-in-json) – Lightness Races in Orbit May 22 '13 at 11:09
  • 20
    I think this is more about readability of the serialized JSON file and _not_ about linebreaks in the loaded data (thus, not a duplicate of [Multiline strings in JSON](http://stackoverflow.com/q/2392766/703040)). Think of it more like using JSON as a configuration file where you have a long string and, for readability, it is helpful to hard-wrap the string in case someone is editing it via a text editor. – zashu Aug 22 '13 at 22:54
  • 2
    @zashu: Most text editors have a soft-wrap function. That's immediately more useful than some hard-coded line width. – Lightness Races in Orbit Feb 17 '14 at 15:50
  • 5
    @LightnessRacesinOrbit running git diffs (or resolving merge conflicts) on files with such long lines is also a pain. – Hubert Kario Nov 20 '15 at 14:20
  • 1
    This IS a dupe a of Multiline string in JSON, citing that OP's question: "*Is it possible to have multi-line strings in JSON? It's **mostly for visual comfort** so I suppose I can just turn word wrap on in my editor, but I'm just kinda curious...*" – Andre Figueiredo Jan 26 '18 at 13:15
  • Possible duplicate of [How do I handle newlines in JSON?](https://stackoverflow.com/questions/42068/how-do-i-handle-newlines-in-json) –  Sep 21 '19 at 22:44
  • Does this answer your question? [Are multi-line strings allowed in JSON?](https://stackoverflow.com/questions/2392766/are-multi-line-strings-allowed-in-json) – Wai Ha Lee Oct 15 '21 at 12:26

5 Answers5

74

Per the specification, the JSON grammar's char production can take the following values:

  • any-Unicode-character-except-"-or-\-or-control-character
  • \"
  • \\
  • \/
  • \b
  • \f
  • \n
  • \r
  • \t
  • \u four-hex-digits

Newlines are "control characters", so no, you may not have a literal newline within your string. However, you may encode it using whatever combination of \n and \r you require.

The JSONLint tool confirms that your JSON is invalid.


And, if you want to write newlines inside your JSON syntax without actually including newlines in the data, then you're doubly out of luck. While JSON is intended to be human-friendly to a degree, it is still data and you're trying to apply arbitrary formatting to that data. That is absolutely not what JSON is about.

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • "_While JSON is intended to be human-friendly to a degree, it is still data..._" There it is. Or, as [Crockford puts it](https://www.json.org/json-en.html), "_[JSON] is easy for humans to read and write. **It is easy for machines to parse and generate**._" [emph mine] One might argue that [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) would be a good addition, but one might have to argue with Crockford (and, natch, individual JSON implementations). – ruffin Sep 02 '20 at 18:26
40

I'm not sure of your exact requirement but one possible solution to improve 'readability' is to store it as an array.

{
  "testCases" :
  {
    "case.1" :
    {
      "scenario" : "this the case 1.",
      "result" : ["this is a very long line which is not easily readble.",
                  "so i would like to write it in multiple lines.",
                  "but, i do NOT require any new lines in the output."]
    }
  }
}

}

Then join it back as the need arrives with

    result.join(" ") 
Brian McAuliffe
  • 2,099
  • 1
  • 16
  • 13
  • 20
    That's unsemantic though. It's an abstraction leak. I would consider a data format specified in this way to be, essentially, broken. – Lightness Races in Orbit Nov 20 '15 at 15:37
  • 4
    I can't imagine designing an API or JSON document this way just to increase string readability (only for debugging reasons I guess) :| .... – Adam Dyga Apr 27 '16 at 14:07
  • " I would consider a data format specified in this way to be, essentially, broken." Consider that this is for a test case, though. If you want to hard-code strings you probably want a properties file. I personally wouldn't store "real" data in JSON in normal circumstances. But if this is a simple way to go from no tests to tests, then go for it. – sf_jeff Dec 31 '16 at 17:02
  • @AdamDyga in fact it could be usefull in order to store the json files into a CVS, where the unit of delta is the line. – yota Jul 14 '17 at 15:38
9

Not pretty good solution, but you can try the hjson tool. It allows you to write text multi-lined in editor and then converts it to the proper valid JSON format.

Note: it adds '\n' characters for the new lines, but you can simply delete them in any text editor with the "Replace all.." function.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
omalyutin
  • 445
  • 7
  • 24
4

I believe it depends on what json interpreter you're using... in plain javascript you could use line terminators

{
  "testCases" :
  {
    "case.1" :
    {
      "scenario" : "this the case 1.",
      "result" : "this is a very long line which is not easily readble. \
                  so i would like to write it in multiple lines. \
                  but, i do NOT require any new lines in the output."
    }
  }
}
Elric Best
  • 51
  • 4
3

As I could understand the question is not about how pass a string with control symbols using json but how to store and restore json in file where you can split a string with editor control symbols.

If you want to store multiline string in a file then your file will not store the valid json object. But if you use your json files in your program only, then you can store the data as you wanted and remove all newlines from file manually each time you load it to your program and then pass to json parser.

Or, alternatively, which would be better, you can have your json data source files where you edit a sting as you want and then remove all new lines with some utility to the valid json file which your program will use.

JustAnotherCurious
  • 2,208
  • 15
  • 32