0

我正在尝试使用李浩毅的 ujson 读取一个 json 字符串。这是字符串:

{
  "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"
        }
      ]
    }
  ]
}

这就是我的阅读方式:

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

但是,返回类型是ujson.Obj,而不是Arr(ArrayBuffer(Obj),正如预期的那样,当我尝试获取时jsonData(0),我得到的是json.Value$InvalidData: Expected ujson.Arr

我问这个问题是因为我尝试使用 ujson 对象来创建一个 upickle 对象,但我不能,我怀疑这是因为这个初始错误。

关于为什么会发生这种情况的任何想法?任何帮助将不胜感激!提前致谢!!

4

1 回答 1

2

JSON 的外部元素不是数组,它是具有单个元素的对象,dataflows其值为数组。试试jsonData("dataflows")(0)

于 2021-07-06T07:21:27.373 回答