How to convert rego.ResultSet to json? #346
Unanswered
archangel78
asked this question in
OPA and Rego
Replies: 1 comment
-
Hi @archangel78, sorry for the delay here. A result contains a list of Expressions, as I understand it, there is an expression for each query. e.g. a query like this:
would have two Expressions. For each expression, there is a value. This is presumably what you want to extract. Perhaps something like this might might be suitable if you just need a JSON result? package main
import (
"context"
"encoding/json"
"fmt"
"github.com/open-policy-agent/opa/rego"
)
func main() {
ctx := context.Background()
r := rego.New(
rego.Module("example.rego", `
package example
foo := { "foo": "bar", "baz": input.x }
`),
rego.Query("data.example.foo"),
)
pq, _ := r.PrepareForEval(ctx)
input := map[string]interface{}{"x": 2}
rs, _ := pq.Eval(ctx, rego.EvalInput(input))
value := rs[0].Expressions[0].Value.(map[string]interface{})
bs, _ := json.MarshalIndent(value, "", " ")
fmt.Println(string(bs))
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Can someone explain what the Items in ExpressionValues mean? (Value, Text and Location). I am trying to convert the contents of this value interface to json but i am unable to. Can someone tell me a clean way to convert it to json and extract the Values in it.
result, err := preparedQry.Eval(ctx, rego.EvalInput(input))
This is how i am getting the resultset
Beta Was this translation helpful? Give feedback.
All reactions