1

I wanted to pass the Scala Map values to JavaScript JSON onject in the Play framework view

I get "SyntaxError: JSON.parse: unexpected character at line 1 column 10 of the JSON data" When I check the following code

val mapTobePassToJs = Map(

      "cg1" -> Map(
        "path" -> "/var/logs/cdr1/cdr",
        "name"  -> "CG-1"

      ),

      "cg2" -> Map(
        "path" -> "/var/logs/cdr2/cdr",
        "name"  -> "CG-2"
      ),

      "cg3" -> Map(
        "path" -> "/var/logs/cdr3/cdr",
        "name"  -> "CG-3"
      ),

      "cg4" -> Map(
        "path" -> "/var/logs/cdr3/cdr",
        "name"  -> "CG-4"
      )
    )

<script type="text/javascript">

        var aChartList = jQuery.parseJSON("@JSONObject(mapTobePassToJs).toString()".replace(/&quot;/g,'"'));

</script>

Anyone please help me

1 Answer 1

3

As you want Json as a result why are you not using Json.obj(). Following would be your structure then.

val mapTobePassToJs = Json.obj(

      "cg1" -> Json.obj(
        "path" -> "/var/logs/cdr1/cdr",
        "name"  -> "CG-1"

      ),

      "cg2" -> Json.obj(
        "path" -> "/var/logs/cdr2/cdr",
        "name"  -> "CG-2"
      ),

      "cg3" -> Json.obj(
        "path" -> "/var/logs/cdr3/cdr",
        "name"  -> "CG-3"
      ),

      "cg4" -> Json.obj(
        "path" -> "/var/logs/cdr3/cdr",
        "name"  -> "CG-4"
      )
    )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.