Search code examples
jsonscalaujsonupickle

scala ujson.read() returns ujson.Obj


I am trying to read a json string using Li Haoyi's ujson. This is the string:

{
  "dataflows": [
    {
      "name": "test",
      "sources": [
        {
          "name": "person_inputs",
          "path": "/data/input/events/person/*",
          "format": "JSON"
        }
      ],
      "transformations": [
        {
          "name": "validation",
          "type": "validate_fields",
          "params": {
            "input": "person_inputs",
            "validations": [
              {
                "field": "office",
                "validations": [
                  "notEmpty"
                ]
              },
              {
                "field": "age",
                "validations": [
                  "notNull"
                ]
              }
            ]
          }
        },
        {
          "name": "ok_with_date",
          "type": "add_fields",
          "params": {
            "input": "validation_ok",
            "addFields": [
              {
                "name": "dt",
                "function": "current_timestamp"


              }
            ]
          }
        }
      ],
      "sinks": [
        {
          "input": "ok_with_date",
          "name": "raw-ok",
          "paths": [
            "/data/output/events/person"
          ],
          "format": "JSON",
          "saveMode": "OVERWRITE"
        },
        {
          "input": "validation_ko",
          "name": "raw-ko",
          "paths": [
            "/data/output/discards/person"
          ],
          "format": "JSON",
          "saveMode": "OVERWRITE"
        }
      ]
    }
  ]
}

And this is how I read it:

val j = os.read(os.pwd/RelPath("src/main/scala/metadata.json"))
val jsonData = ujson.read(j)

But, the return type is ujson.Obj, and not Arr(ArrayBuffer(Obj), as expected, such that when I try to get jsonData(0), what I get is json.Value$InvalidData: Expected ujson.Arr.

I am asking this question because I have tried to use the ujson object to create a upickle object, but I cannot, and I suspect it is because of this initial error.

Any ideas of why this happens? Any help would be greatly appreciated! Thanks in advance!!


Solution

  • The outer element of your JSON is not an array, it is an object with a single element dataflows whose value is an array. Try jsonData("dataflows")(0).