1

I am trying to read rego format of policy and save into json format. However could not find a way to read using "go" package of rego. Any suggestions/advise ?

vnn
  • 31
  • 1
  • 4

2 Answers2

0

Converting Rego to JSON is like trying to convert GoLang to JSON. Rego is not a subset of JSON so this task is not easy. Rego is a programing language of its own and has many built-in features and functions that would not be easily expressed in JSON.

Perhaps you could define the goal you want to accomplish with the JSON data and there may be another solution?

Cheers, Peter

  • 1
    Thanks for the response Peter. I was hoping to give flexibility to user to specify json inputs (backend to construct rego with known policy attributes) or rego policy as input. In both cases, i was hoping we could return json as well as rego policy as response. I thought there might be way to extract policy keywords and corresponding value from rego text into json format. But i agree with you ... it might be difficult to parse into json. – vnn Oct 04 '21 at 20:27
  • Hi vnn, Is there any progress on the same. we also have the same need :) – Abdul Kader Jeelani Jun 10 '22 at 12:19
  • According to this statement from Torin Sandall Rego is a superset of JSON. https://github.com/open-policy-agent/opa/issues/2817 – point Jun 28 '23 at 15:12
0

You can convert the AST of a Rego file to JSON using the following command:

opa parse --format json file.rego

I'm not sure if this format is stable though.

JW.
  • 2,081
  • 1
  • 20
  • 24