Can a JSON value contain a multiline string

I am writing a JSON file that a Java program will read. 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" } } } 
+71
json multiline
May 22 '13 at 11:03
source share
6 answers

Check out the specs ! A JSON char grammar product can take the following values:

  • any-unicode-characters except- " -or- \ -or-character control
  • \"
  • \\
  • \/
  • \b
  • \f
  • \n
  • \r
  • \t
  • \u four hexadecimal digits

Newlines are control characters, so no, you may not have a literal new line inside your line. However, you can encode it using any combination of \n and \r that you need.

The JSONLint tool confirms that your JSON is invalid.




Update: And if you want to write newline characters inside your JSON syntax without actually including lines in the data, then you are even doubly out of luck. While JSON is designed to be somewhat human-friendly, it is still data, and you are trying to apply arbitrary formatting to that data. This is absolutely not what JSON is about.

+38
May 22 '13 at 11:08
source share

I'm not sure about your exact requirements, but one of the possible solutions 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."] } } } } 

Reconnect if necessary with

 result.join(" ") 
+22
Jan 24 '14 at 13:10
source share

As I understand it, the question is not how to pass the string using control characters using json , but how to save and restore json in a file where you can split the string with editor control characters.

If you want to save a multi-line string in a file, then your file will not save a valid json object. But if you use only json files only in your program, then you can store data as you wish and delete all new lines from the file manually each time you load it into your program and then transfer json parser.

Or, alternatively, what would be better, you can have your json source files where you edit sting as you like, and then delete all new lines with some utility in the actual json file that your program will use.

+3
May 22 '13 at 11:58
source share

This is implemented as a writer because there can be several output characters for a single character. I could not imagine it as a reader. Quite a difficult task, but quite expandable.

 String multilineJson = "{\n" + "prop1 = \"value1\",\n" + "prop2 = \"multi line\n" + "value2\"\n" + "}\n"; String multilineJsonExpected = "{\n" + "prop1 = \"value1\",\n" + "prop2 = \"multi line\\nvalue2\"\n" + "}\n"; StringWriter sw = new StringWriter(); JsonProcessor jsonProcessor = new JsonProcessor(sw); jsonProcessor.write(multilineJson); assertEquals(multilineJsonExpected, sw.toString()); 

Implementation

 public class JsonProcessor extends FilterWriter { private char[] curr; private int currIdx; private boolean doubleQuoted; public JsonProcessor(Writer out) { super(out); } @Override public void write(String str) throws IOException { char[] arr = str.toCharArray(); write(arr, 0, arr.length); } @Override synchronized public void write(char[] cbuf, int off, int len) throws IOException { curr = Arrays.copyOfRange(cbuf, off, len - off); for (currIdx = 0; currIdx < curr.length; currIdx++) { processChar(); } } private void processChar() throws IOException { switch (currentChar()) { case '"': processDoubleQuotesSymbol(); break; case '\n': case '\r': processLineBreakSymbol(); break; default: write(currentChar()); break; } } private void processDoubleQuotesSymbol() throws IOException { doubleQuoted = !doubleQuoted; write('"'); } private void processLineBreakSymbol() throws IOException { if (doubleQuoted) { write('\\'); write('n'); if (lookAhead() == '\n' || lookAhead() == '\r') { currIdx++; } } else { write(currentChar()); } } private char currentChar() { return curr[currIdx]; } private char lookAhead() { if (currIdx >= curr.length) { return 0; } return curr[currIdx + 1]; } } 
0
Jun 15 '16 at 13:51
source share

Not a good solution, but you can try the hjson tool. Link This allows you to write multi-line text in the editor and then convert them to the correct valid JSON format. Note: it adds \ n 'characters for newlines, but you can simply delete them in any text editor using the "Replace All .." function.

PS There should be a comment on the question, but there is not enough repo, sorry.

0
Jun 30 '16 at 8:25
source share

I believe it depends on which json interpreter you use ... in plain javascript you can use string 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." } } } 
0
Nov 21 '16 at 17:34
source share



All Articles