EvaluateJsonPath unable to return a scalar
Asked Answered
B

1

8

I'm trying to extract a value from JSON to a flowfile-attribute. When I run the EvaluateJsonPath processor I get an error stating

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

Input JSON looks like this:

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

What I think is happening is that the processor is returning ["https://path/to/media"] rather than just the string.

This is what I get if a evaluate to flowfile-content rather than an attribute. Why? What can I do to fix it?

Boarish answered 23/10, 2018 at 3:20 Comment(0)
C
15

Change the Return Type property value to json in EvaluateJsonPath processor, if you are extracting as flowfile-attribute

Return Type property description:

Indicates the desired return type of the JSON Path expressions. Selecting 'auto-detect' will set the return type to 'json' for a Destination of 'flowfile-content', and 'scalar' for a Destination of 'flowfile-attribute'.

enter image description here 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.

In Addition you can use FlattenJson processor(seperator '_') to flatten-out all nested json then use Return Type as auto detect in EvaluateJsonPath processor.

EvaluateJsonConfigs: enter image description here

Output: We are going to have attribute value without enclosing in an array enter image description here

Caffrey answered 23/10, 2018 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.