Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read/write json data using starlark? #215

Closed
tamalsaha opened this issue Jul 2, 2019 · 7 comments
Closed

How to read/write json data using starlark? #215

tamalsaha opened this issue Jul 2, 2019 · 7 comments

Comments

@tamalsaha
Copy link

Is there any example that shows how to read write json data using starlark ?

@alandonovan
Copy link
Contributor

We don't yet have a module for JSON access, though there is clearly a need. I posted a first draft in #179 but have not had time to finish it off. I hope to return to it in the near future, in combination with support for reading and writing protocol messages. I think the combination will open many doors for new tooling.

@tamalsaha
Copy link
Author

I would like to see the json support being added. This will make it an interesting embedded scripting language in the Kubernetes context where all api responses are json. Looking forward to this.

@errordeveloper
Copy link

I'd like to build a complex object (like a Kubernetes resource) in a starlark script and return it back to my Go program as a JSON script or something that can be easily parsed in the same manager as JSON.

@tamalsaha
Copy link
Author

@errordeveloper , for Kubernetes YAML, the better option will the kyaml library which preserves the comments, yaml order etc. There are some examples here:
https://github.com/kubernetes-sigs/kustomize/blob/master/kyaml/starlark/example_test.go

@errordeveloper
Copy link

@tamalsaha interesting, didn't expect that kustomize would add starlark in, I was previously told it would be against the original design... Will have a good look, thanks! For now I am actually quite keen to try out CUE for the use-case at hand.

@dilyevsky
Copy link

@tamalsaha maybe https://github.com/cruise-automation/isopod would be of interest. It uses implementation in skycfg (https://github.com/stripe/skycfg/blob/2a2d2baed02b9ef17e98fdbdd1d7255e9adfbad3/internal/go/skycfg/json_write.go#L45) to render json for CRD types (but normally uses Protobuf wire format where supported)

@alandonovan
Copy link
Contributor

PR #179 is committed, so you can start to use json.encode and json.decode within your Starlark application. Of course Starlark programs still cannot read or write files unless the application provides a way to do that.

I may move the Go code into a lib/json directory, which is a breaking API change but not a breaking Starlark language change.

Please try it out and report any problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants