Trouble with scalajs.js.JSON.parse

Dear all,

I have a 3rd party JSON data of the shape in the P.S. When I use scalajs.js.JSON.parse against it, I get unexpetdly variable behaviour. For example, taking text to be what’s in the P.S. and with

val json = scalajs.js.JSON.parse(text)
val item1 = json.pop()
val id1 = item1._id.toString

id1 evaluates to the string 1. But,

val node1 = item1.node.sourceType

throws Uncaught TypeError: Cannot read property 'sourceType' of undefined. I was extecing the string script here. Furthermore,

val successors1 = item1._successors.pop().toString

evalutaes successors1 to the string undefined for me. I was rather expeting to get the string 1 here. Does anyone know what I am missing?

TIA,
–Hossein

P.S.

[
    {
        "node": {
            "type": "Program",
            "body": [
                {
                    "type": "ExpressionStatement",
                    "expression": {
                        "type": "Literal",
                        "value": 123,
                        "raw": "123",
                        "loc": {
                            "start": {
                                "line": 1,
                                "column": 0
                            },
                            "end": {
                                "line": 1,
                                "column": 3
                            }
                        },
                        "tag": 2
                    },
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 0
                        },
                        "end": {
                            "line": 1,
                            "column": 3
                        }
                    },
                    "tag": 1
                }
            ],
            "sourceType": "script",
            "loc": {
                "start": {
                    "line": 1,
                    "column": 0
                },
                "end": {
                    "line": 1,
                    "column": 3
                }
            },
            "tag": 0,
            "funScopeDecls": {}
        },
        "benv": {
            "_map": {
                "_buckets": [
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null
                ]
            },
            "_global": true
        },
        "store": null,
        "lkont": [],
        "kont": null,
        "_successors": [
            1
        ],
        "_sstorei": 0,
        "_id": 0
    },
    {
        "value": {
            "type": 8,
            "as": {
                "_arr": []
            }
        },
        "store": null,
        "lkont": [],
        "kont": null,
        "_successors": [],
        "_sstorei": 0,
        "_id": 1
    }
]

Any chance you could give library dependencies and other items needed so that those of us that might want to explore your code example, and maybe help, could do so with minimal research of our own to get a working example?

Sure. The output is:

id1 = 1, sstorei1 = 0, lkont1 = [], successors1 = [undefined]

for the files in the P.S. I would rather like the output to be

id1 = 1, sstorei1 = 0, lkont1 = [], successors1 = [1]

with no error message (exception) for node1.

Any better now?

TIA,
–Hossein

P.S.

  • webpage/Demo.scala:
package webpage

import org.scalajs.dom
import org.scalajs.dom.{Node, Element}
import scalajs.js
import scalajs.js.annotation.JSExport
import scalatags.JsDom.all._
import dom.html

@JSExport
object Demo extends{
  @JSExport
  def main(target: html.Div) = {
    import dom.ext._
    import scala.scalajs
    .concurrent
    .JSExecutionContext
    .Implicits
    .runNow


    val text = """ [{"node":{"type":"Program","body":[{"type":"ExpressionStatement","expression":{"type":"Literal","value":123,"raw":"123","loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},"tag":2},"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},"tag":1}],"sourceType":"script","loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},"tag":0,"funScopeDecls":{}},"benv":{"_map":{"_buckets":[null,null,null,null,null,null,null,null,null,null,null,null,null]},"_global":true},"store":null,"lkont":[],"kont":null,"_successors":[1],"_sstorei":0,"_id":0},{"value":{"type":8,"as":{"_arr":[]}},"store":null,"lkont":[],"kont":null,"_successors":[],"_sstorei":0,"_id":1}] """
    val json = js.JSON.parse(text)
    val item1 = json.pop()
    val id1 = item1._id.toString
//    val node1 = item1.node.sourceType //Uncaught TypeError: Cannot read property 'sourceType' of undefined
    val sstorei1 = item1._sstorei.toString
    val lkont1 = item1.lkont.toString
    val successors1 = item1._successors.pop().toString

    target.appendChild(
      pre("id1 = " + id1 +
          ", sstorei1 = " + sstorei1 +
          ", lkont1 = [" + lkont1 + "]" +
          ", successors1 = [" + successors1 + "]"
          ).render)
  }
}
  • index-dev.html:
<!DOCTYPE html>
<html>
<head>
  <title>Demo the JSON</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body style="margin: 0px">

<div>
    <div style="display: block" id="div" width="255" height="255"/>
</div>

<script type="text/javascript" src="../example-fastopt.js"></script>
<script type="text/javascript" src="/workbench.js"></script>
<script>
    webpage.Demo().main(document.getElementById('div'));
</script>
</body>
</html>
  • project/build.sbt:
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.13")

addSbtPlugin("com.lihaoyi" % "workbench" % "0.3.0")
  • build.sbt:
enablePlugins(ScalaJSPlugin, WorkbenchPlugin)

name := "Example"

version := "0.1-SNAPSHOT"

scalaVersion := "2.11.8"

libraryDependencies ++= Seq(
  "org.scala-js" %%% "scalajs-dom" % "0.9.1",
  "com.lihaoyi" %%% "scalatags" % "0.6.2"
)