0

In the following JSON document, how can I escape the newline character? At present, the document fails validation.

{
    "questionId" : 1,
    "text" : "Given the following code, what gets printed?",
    "questype" : "RADIO_BUTTON",
    "issourcecode" : true,
    "sourcecode" : "def patternMatcher(list: List[Int]) = list match { \\n\\n
                      case Nil => \"The List is empty\" \n
                      case x :: Nil => s\"The List contained $x\" \n
                      case x :: xss => s\"The List contained $x and $xss\" \n
                      case _ => \"None of the above\" \n
                    }
                    patternMatcher(List(1,2,3,4)) -- What gets printed here?",
    "examId" : 1000,
    "answers" : [
        {
            "id" : 1,
            "text" : "The List is empty",
            "isCorrectAnswer" : false
        },
        {
            "id" : 2,
            "text" : "The List contained 1",
            "isCorrectAnswer" : false
        },
        {
            "id" : 3,
            "text" : "The List contained 1 and List(2,3,4)",
            "isCorrectAnswer" : true
        },
        {
            "id" : 4,
            "text" : "None of the above",
            "isCorrectAnswer" : false
        }
    ]
}

It throws error at Line 6, where the newline character is located!

I later changed the JSON to the following:

{
    "questionId" : 1,
    "text" : "Given the following code, what gets printed?",
    "questype" : "RADIO_BUTTON",
    "issourcecode" : true,
    "sourcecode" : "def patternMatcher(list: List[Int]) = list match {\\n
                      case Nil => \"The List is empty\" \\n
                      case x :: Nil => s\"The List contained $x\" \\n
                      case x :: xss => s\"The List contained $x and $xss\" \\n
                      case _ => \"None of the above\" \\n
                    }
                    patternMatcher(List(1,2,3,4)) -- What gets printed here?",
    "examId" : 1000,
    "answers" : [
        {
            "id" : 1,
            "text" : "The List is empty",
            "isCorrectAnswer" : false
        },
        {
            "id" : 2,
            "text" : "The List contained 1",
            "isCorrectAnswer" : false
        },
        {
            "id" : 3,
            "text" : "The List contained 1 and List(2,3,4)",
            "isCorrectAnswer" : true
        },
        {
            "id" : 4,
            "text" : "None of the above",
            "isCorrectAnswer" : false
        }
    ]
}

I used the following website to validate my JSON and it always fails with the following error:

http://www.freeformatter.com/json-validator.html

The JSON input is NOT valid in JavaScript, unterminated string literal (At line #6), (At position #73)
joesan
  • 13,963
  • 27
  • 95
  • 232

1 Answers1

0

Ok! As soon as I changed to reflect the following, it worked:

"sourcecode" : "def patternMatcher(list: List[Int]) = list match {\\n  case Nil => \"The List is empty\" \\n  case x :: Nil => s\"The List contained $x\" \\n  case x :: xss => s\"The List contained $x and $xss\" \\n  case _ => \"None of the above\" \\n}patternMatcher(List(1,2,3,4)) -- What gets printed here?"

I had to have it in a single line. Strange!

joesan
  • 13,963
  • 27
  • 95
  • 232