Skip to content
Advertisement

How to extract the field from JSON object with QueryRecord

I have been struggling with this problem for a long time. I need to create a new JSON flowfile using QueryRecord by taking an array (field ref) from input JSON field refs and skip the object field as shown in example below:

Input JSON flowfile

{
    "name": "name1",
    "desc": "full1",
    "refs": {
        "ref": [
            {
                "source": "source1",
                "url": "url1"
            },
            {
                "source": "source2",
                "url": "url2"
            }
        ]
    }
}

QueryRecord configuration

JSONTreeReader setup as Infer Schema and JSONRecordSetWriter

select name, description, (array[rpath(refs, '//ref[*]')]) as sources from flowfile

Output JSON (need)

{
    "name": "name1",
    "desc": "full1",
    "references": [
         {
             "source": "source1",
             "url": "url1"
         },
         {
             "source": "source2",
             "url": "url2"
         }
    ]
}

But got error: QueryRecord Failed to write MapRecord[{references=[Ljava.lang.Object;@27fd935f, description=full1, name=name1}] with schema ["name" : "STRING", "description" : "STRING", "references" : "ARRAY[STRING]"] as a JSON Object due to java.lang.ClassCastException: null

Advertisement

Answer

Solution!: use JoltTransformJSON to transform JSON by Jolt specification. About this specification.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement