简体   繁体   English

EvaluateJsonPath无法返回标量

[英]EvaluateJsonPath unable to return a scalar

I'm trying to extract a value from JSON to a flowfile-attribute . 我正在尝试从JSON提取一个值到flowfile-attribute When I run the EvaluateJsonPath processor I get an error stating 当我运行EvaluateJsonPath处理器时,出现错误提示

"Unable to get a scalar value for expression $..fields.storyBoard.stringValue . "Unable to get a scalar value for expression $..fields.storyBoard.stringValue

Input JSON looks like this: 输入JSON如下所示:

{
  "name" : "projects/fakedims-0000/databases/(default)/documents/device/0000",
  "fields" : {
    "reportKey" : {
      "stringValue" : "abc123"
    },
    "dateOccured" : {
      "timestampValue" : "2018-10-14T04:00:00Z"
    },
    "storyBoard" : {
      "stringValue" : "https://path/to/media"
    },
    "new" : {
      "integerValue" : "25"
    },
    "name" : {
      "stringValue" : "device one"
    },
    "location" : {
      "geoPointValue" : {
        "latitude" : -78.413751,
        "longitude" : 38.156487
      }
    }
  },
  "createTime" : "2018-10-19T00:02:26.209335Z",
  "updateTime" : "2018-10-19T22:22:24.382136Z"
}

The JSONPath expression is $..fields.storyBoard.stringValue JSONPath表达式为$..fields.storyBoard.stringValue fields.storyBoard.stringValue

What I think is happening is that the processor is returning ["https://path/to/media"] rather than just the string. 我认为正在发生的事情是处理器正在返回["https://path/to/media"]而不只是字符串。

This is what I get if a evaluate to flowfile-content rather than an attribute. 如果对flowfile-content而不是属性进行评估,这就是我得到的。 Why? 为什么? What can I do to fix it? 我该如何解决?

Change the Return Type property value to json in EvaluateJsonPath processor, if you are extracting as flowfile-attribute 如果要提取为flowfile-attribute,则在EvaluateJsonPath处理器中将Return Type属性值更改为json

Return Type property description: 返回类型属性说明:

Indicates the desired return type of the JSON Path expressions. 指示所需的JSON Path表达式的返回类型。 Selecting ' auto-detect ' will set the return type to ' json ' for a Destination of ' flowfile-content ', and ' scalar ' for a Destination of ' flowfile-attribute '. 选择“ 自动检测 ”将针对“ flowfile-content ”的目的地将返回类型设置为“ json ”,对于“ flowfile-attribute ”的目的地将其返回类型设置为“ scalar ”。

在此处输入图片说明 As you are trying to extract nested key not the key on the root level(ex:name,createTime..), that's the reason why we need to configure the Return Type as Json not as scalar . 当您尝试提取嵌套键而不是根级别的键(例如:name,createTime ..)时,这就是为什么我们需要将返回类型配置为Json而不是标量的原因

In Addition you can use FlattenJson processor(seperator '_') to flatten-out all nested json then use Return Type as auto detect in EvaluateJsonPath processor. 另外,您可以使用FlattenJson处理器(分隔符“ _”)将所有嵌套的json扁平化,然后在EvaluateJsonPath处理器中将Return Type as auto detect

EvaluateJsonConfigs: EvaluateJsonConfigs: 在此处输入图片说明

Output: We are going to have attribute value without enclosing in an array 输出:我们将拥有属性值,而无需将其包含在数组中 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM