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

Document variable assignment to a JSON (Dict) attribute #1129

Closed
kellyjonbrazil opened this issue May 18, 2022 · 9 comments
Closed

Document variable assignment to a JSON (Dict) attribute #1129

kellyjonbrazil opened this issue May 18, 2022 · 9 comments

Comments

@kellyjonbrazil
Copy link

In the documentation it shows how to read JSON into a variable and write it out, but doesn't show how you can select a portion of the JSON and assign that to a variable as a scalar, array, or map.

https://github.com/oilshell/oil/blob/master/doc/json.md

I'm working on adding examples of how to use jc in alternative shells. I was able to assign JSON to a variable, but I'm not sure the recommended way to further filter the variable and assign that value to another variable. This is what I came up with:

$ dig www.google.com | jc --dig | json read myvar
$ json write (myvar[0]['answer'][0]['data']) | json read mydata
$ echo $mydata
64.233.185.104

https://github.com/kellyjonbrazil/jc/wiki/Using-jc-With-Different-Shells

Is this a good way to do it or is there a better way to slice the data once it is read into Oil?

@andychu
Copy link
Contributor

andychu commented May 19, 2022

Thanks for the feedback! I pushed a change with some example, which will be out with the next release.

A full reference will have to wait for #1130

@kellyjonbrazil
Copy link
Author

Thank you!

@kellyjonbrazil
Copy link
Author

I do have a followup question:

In my example above I'm basically converting the Oil variable myvar into JSON so I can filter it and read it into another Oil variable mydata via STDOUT. This seems inefficient. I was thinking I could do something like:

oil$ = myvar[0]['answer'][0]['data']
(Str)   '142.250.138.104'
oil$ mydata=myvar[0]['answer'][0]['data']

But this doesn't seem to work as it assigns the literal string myvar[0][answer][0][data] instead of the data that expression is pointing to:

oil$ echo $mydata
myvar[0][answer][0][data]

I've tried different variations/styles of setting variables but I can't seem to get what I'm looking for. Is there a way to do this natively without translating back and forth between JSON and passing to/from STDOUT?

@andychu
Copy link
Contributor

andychu commented May 19, 2022

Ahh so this is actually a side effect of fixing #937 in the most recent release release.

It used to be that we disallowed shell style assignment in Oil

a=b   #  LEGACY, no space

and allowed

a = 'b'  # Oil assignment, with spaces.  Disallow the previous style because it's too close

Now it has to be

const a = 'b'
var x = 'y'
setvar x ='z'

So as a side effect now allow a=b again in bin/oil.

I think we can simply disallow it again ... But not allow a = 'b' either,

Thanks for the feedback! I was thinking about this too

@kellyjonbrazil
Copy link
Author

Beautiful - yes, the var keyword is what I needed, thanks!

oil$ var mydata = myvar[0]['answer'][0]['data']
oil$ echo $mydata
142.250.138.104

@andychu
Copy link
Contributor

andychu commented May 20, 2022

The doc is released:

https://www.oilshell.org/release/0.10.1/doc/json.html

https://oilshell.zulipchat.com/#narrow/stream/325160-oil-discuss-public/topic/Oil.200.2E10.2E1.20Released!

I have an idea for shopt --set parse_only_oil to disallow shell-style assignment. Thanks for the feedback!

@andychu andychu closed this as completed May 20, 2022
@andychu
Copy link
Contributor

andychu commented May 20, 2022

Let me know if you see anything else. Generally the expression language is like Python

This doc is not yet polished, but it gives a "diff" vs Python:

https://www.oilshell.org/release/latest/doc/oil-vs-python.html

@kellyjonbrazil
Copy link
Author

I ended up turning this into a blog post. Let me know if you would like any updates to the Oil section. Thanks!

https://blog.kellybrazil.com/2022/05/19/working-with-json-in-various-shells/

@andychu
Copy link
Contributor

andychu commented May 20, 2022

Great post, thanks! I'm slightly surprised at how much convergence there is :)

This reminds me a bit of issue #736 -- I feel like a shell could make it easier to install / try new tools without root access, and without messing with PATH. That always seems to be a barrier for trying new tools

That and versioning of the tools

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

No branches or pull requests

2 participants