You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@nifi.apache.org by l vic <lv...@gmail.com> on 2018/09/05 13:27:41 UTC
parsing json array in ExecuteScript?
I have json array attribute:
mylist
:[{"id":10,"info":"2am-3am"},{"id":11,"info":"3AM-4AM"},{"id":12,"info":"4am-5am"}]
How can I parse it to name/value pairs in groovy script?
Thanks,
Re: parsing json array in ExecuteScript?
Posted by Mike Thomsen <mi...@gmail.com>.
> How can I parse it to name/value pairs in groovy script?
I would recommend getting the Groovy binary distribution (we use 2.4.X) and
experimenting with that. Aside from us throwing in a few of the NiFi APIs,
it's a standard Groovy environment. You'll flatten the learning curve on
writing these scripts if you approach it that way.
Thanks,
Mike
On Wed, Sep 5, 2018 at 9:34 AM Matt Burgess <ma...@apache.org> wrote:
> Are you trying to collect them into a single data structure, or just
> iterate over the key/value pairs? For the former, you'd need to change
> the key names somehow, since each object in the array has the same
> keys. For the latter, you can use each() to iterate over any
> collection:
>
> mylist.each {arrayEntry -> arrayEntry.each {k,v -> println "$k = $v"}}
>
> Note that the each() for a Map allows you to get the key and value
> separately in the closure, rather than iterating over Map.Entry
> objects and having to call it.key and it.value.
>
> Regards,
> Matt
> On Wed, Sep 5, 2018 at 9:27 AM l vic <lv...@gmail.com> wrote:
> >
> > I have json array attribute:
> > mylist
> :[{"id":10,"info":"2am-3am"},{"id":11,"info":"3AM-4AM"},{"id":12,"info":"4am-5am"}]
> > How can I parse it to name/value pairs in groovy script?
> > Thanks,
>
Re: parsing json array in ExecuteScript?
Posted by Matt Burgess <ma...@apache.org>.
Are you trying to collect them into a single data structure, or just
iterate over the key/value pairs? For the former, you'd need to change
the key names somehow, since each object in the array has the same
keys. For the latter, you can use each() to iterate over any
collection:
mylist.each {arrayEntry -> arrayEntry.each {k,v -> println "$k = $v"}}
Note that the each() for a Map allows you to get the key and value
separately in the closure, rather than iterating over Map.Entry
objects and having to call it.key and it.value.
Regards,
Matt
On Wed, Sep 5, 2018 at 9:27 AM l vic <lv...@gmail.com> wrote:
>
> I have json array attribute:
> mylist :[{"id":10,"info":"2am-3am"},{"id":11,"info":"3AM-4AM"},{"id":12,"info":"4am-5am"}]
> How can I parse it to name/value pairs in groovy script?
> Thanks,
Re: parsing json array in ExecuteScript?
Posted by Ed B <bd...@gmail.com>.
I would suggest to use more convenient ways to achieve the same:
Split array into separate flow files, then use "EvaluateJsonPath" to
extract all the values into attributes (in case you know keys in advance).
If you don't know, when you can use first JoltTransformJSON, to generate
[{"name":"id","value":"10"},{"name":"info","value":"2am-3am"}.....]
then you can split such arrays and evaluateJsonPath from "name" and "value"
into ff attributes.
On Wed, Sep 5, 2018 at 9:27 AM l vic <lv...@gmail.com> wrote:
> I have json array attribute:
> mylist
> :[{"id":10,"info":"2am-3am"},{"id":11,"info":"3AM-4AM"},{"id":12,"info":"4am-5am"}]
> How can I parse it to name/value pairs in groovy script?
> Thanks,
>