Skip to content

Commit a905a01

Browse files
abhijitWakchaure“jdattatr-tibco”jdattatr-tibcovnalawad-tibco
authored
Add JQ function to JSON category (#165)
* added new jqFunction * return array always (#163) Co-authored-by: “jdattatr-tibco” <“[email protected]”> * Added valueType to json.jq return type * updated README for jq --------- Co-authored-by: “jdattatr-tibco” <“[email protected]”> Co-authored-by: jdattatr-tibco <[email protected]> Co-authored-by: Vijay Nalawade <[email protected]>
1 parent e81098f commit a905a01

File tree

5 files changed

+121
-3
lines changed

5 files changed

+121
-3
lines changed

function/json/README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,21 @@ Set the value of existing key or add new key and set it's value in a json object
134134

135135
| Arg | Type | Description |
136136
|:-----------|:-------|:------------------------|
137-
| jsonObject | any | The updated json object |
137+
| jsonObject | any | The updated json object |
138+
139+
140+
## jq()
141+
Process the JSON inputs based on a provided query and return the filtered output
142+
143+
### Input Args
144+
145+
| Arg | Type | Description |
146+
|:---------|:-------|:-----------------------------------|
147+
| object | any | The json object |
148+
| query | string | Query to perform |
149+
150+
### Output
151+
152+
| Arg | Type | Description |
153+
|:-----------|:-------|:------------------------|
154+
| output | array | result of the jq filter |

function/json/descriptor.json

+23-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,28 @@
139139
"return": {
140140
"type": "any"
141141
}
142+
},
143+
{
144+
"name": "jq",
145+
"description": "process the JSON inputs based on a provided query and return the filtered array output",
146+
"example": "json.jq(inputJSON, \"query\") => [output1, output2 ...]",
147+
"args": [
148+
{
149+
"name": "inputJSON",
150+
"type": "any"
151+
},
152+
{
153+
"name": "query",
154+
"type": "string"
155+
}
156+
],
157+
"return": {
158+
"type": "array",
159+
"valueType": "any"
160+
},
161+
"display": {
162+
"visible": true
163+
}
142164
}
143165
]
144-
}
166+
}

function/json/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/project-flogo/contrib/function/json
33
go 1.16
44

55
require (
6+
github.com/itchyny/gojq v0.12.13
67
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
78
github.com/project-flogo/core v1.4.0
89
github.com/stretchr/testify v1.7.0

function/json/go.sum

+14-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ github.com/araddon/dateparse v0.0.0-20190622164848-0fb0a474d195/go.mod h1:SLqhdZ
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
77
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
89
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
10+
github.com/itchyny/gojq v0.12.13 h1:IxyYlHYIlspQHHTE0f3cJF0NKDMfajxViuhBLnHd/QU=
11+
github.com/itchyny/gojq v0.12.13/go.mod h1:JzwzAqenfhrPUuwbmEz3nu3JQmFLlQTQMUcOdnu/Sf4=
12+
github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE=
13+
github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8=
914
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
1015
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1116
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1217
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1318
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1419
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
20+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
21+
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
1522
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852 h1:Yl0tPBa8QPjGmesFh1D0rDy+q1Twx6FyU7VWHi8wZbI=
1623
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852/go.mod h1:eqOVx5Vwu4gd2mmMZvVZsgIqNSaW3xxRThUJ0k/TPk4=
1724
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -21,6 +28,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
2128
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2229
github.com/project-flogo/core v1.4.0 h1:1WJ7tR2TwEMf7Oe0yPZXM9pN7MywMJffNagpL8y9Pxw=
2330
github.com/project-flogo/core v1.4.0/go.mod h1:fapTXUhLxDeAHyb6eMkuwnYswO8FpZJAMat055QVdJE=
31+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
32+
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
2433
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
2534
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2635
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@@ -49,20 +58,24 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
4958
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
5059
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5160
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
61+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
62+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5263
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5364
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
5465
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
5566
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
5667
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs=
5768
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
5869
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
70+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5971
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6072
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
6173
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6274
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
6375
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
6476
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
65-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
6677
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
78+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
79+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6780
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
6881
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=

function/json/jq.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package json
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/itchyny/gojq"
7+
"github.com/project-flogo/core/data"
8+
"github.com/project-flogo/core/data/coerce"
9+
"github.com/project-flogo/core/data/expression/function"
10+
)
11+
12+
func init() {
13+
function.Register(&jq{})
14+
}
15+
16+
type jq struct {
17+
}
18+
19+
// Name returns the name of the function
20+
func (jq) Name() string {
21+
return "jq"
22+
}
23+
24+
// Sig returns the function signature
25+
func (jq) Sig() (paramTypes []data.Type, isVariadic bool) {
26+
return []data.Type{data.TypeAny, data.TypeString}, false
27+
}
28+
29+
// Eval executes the function
30+
func (jq) Eval(params ...interface{}) (interface{}, error) {
31+
32+
var err error
33+
var inputJSON interface{}
34+
35+
//1st parameter will be the input json
36+
inputJSON, err = coerce.ToAny(params[0])
37+
if err != nil {
38+
return nil, fmt.Errorf("unable to coerce input JSON [%+v]: %s", params[0], err.Error())
39+
40+
}
41+
42+
//2nd parameter will be the query
43+
inputQuery, err := coerce.ToString(params[1])
44+
if err != nil {
45+
return nil, fmt.Errorf("unable to coerce the query to string: %s", err.Error())
46+
}
47+
query, err := gojq.Parse(inputQuery)
48+
if err != nil {
49+
return nil, err
50+
}
51+
var result []interface{}
52+
iter := query.Run(inputJSON)
53+
for {
54+
v, ok := iter.Next()
55+
if !ok {
56+
break
57+
}
58+
if err, ok := v.(error); ok {
59+
return nil, fmt.Errorf("error running the query : %s", err)
60+
}
61+
result = append(result, v)
62+
}
63+
64+
return result, nil
65+
}

0 commit comments

Comments
 (0)