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

Dump shell variables #789

Open
ghost opened this issue May 17, 2015 · 5 comments
Open

Dump shell variables #789

ghost opened this issue May 17, 2015 · 5 comments

Comments

@ghost
Copy link

ghost commented May 17, 2015

Given this example file

{
  "upload_date": 20141111,
  "protocol": "http",
  "extractor": "soundcloud",
  "duration": 233
}

I would like to dump these “variables” so that they can be used with a shell
script. Output would be like this

upload_date=20141111
protocol=http
extractor=soundcloud
duration=233

This command gets me close, but does not include the variable (key) names:

$ jq -r '.[] | @sh' example.json
20141111
'http'
'soundcloud'
233
@ghost
Copy link
Author

ghost commented May 17, 2015

This works for simple values

$ jq -r 'to_entries | map("\(.key)=\(.value)") | .[]' example.json
duration=233
extractor=soundcloud
protocol=http
upload_date=20141111

This works for complex values

jq -r 'to_entries | map("\(.key)=\(.value | @sh)") | .[]'

This works to filter certain keys

jq -r '{duration, protocol} | to_entries | map("\(.key)=\(.value | @sh)") | .[]'

Example

However, I am still interested if this can be done with simpler commands.

@nicowilliams
Copy link
Contributor

It might be nice to add a tosh builtin that does this. There are things to decide (traverse recursively? If so how to encode paths into shell variable names decently? e.g., {a:{b:1} | tosh might output nothing, or a__b=1).

@netj
Copy link

netj commented Dec 14, 2015

I ended up creating a shell script called jq2sh that takes as input arguments the variable name and jq expression to generate shell variable definitions. Since you'll most likely consume the generated shell script from the shell itself, I thought it was more intuitive to create it as a handy command that can be called with the essential bit of jq.

For example, the following invocation:

$ jq2sh \
    DEEPDIVE_COMPUTER='.deepdive.computer' \
    DEEPDIVE_COMPUTER_CONFIG='.deepdive.computers[.deepdive.computer]' \
    #

produces the following output:

DEEPDIVE_COMPUTER=local
DEEPDIVE_COMPUTER_CONFIG='{"type":"local"}'

for a JSON supplied as standard input:

{"deepdive":{"computer":"local", "computers":{"local":{"type":"local"}, ...}, ...}}

This will typically be used in an eval "$(jq2sh ... <input.json)".

@nicowilliams
Copy link
Contributor

I do something similar with a shell function I call jqbind, which I invoke as jqbind input.json foo_varname '.jq.expression' ..., and it wraps the eval so it's pretty.

@nicowilliams
Copy link
Contributor

@netj Since you asked for it on IRC (in relation to this issue, IIRC), see #1045.

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

3 participants