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

Feature request: make @sh work on objects #1947

Open
Sec42 opened this issue Jul 15, 2019 · 2 comments
Open

Feature request: make @sh work on objects #1947

Sec42 opened this issue Jul 15, 2019 · 2 comments

Comments

@Sec42
Copy link

Sec42 commented Jul 15, 2019

Currently jq can not apply the "sh" escaping to objects:

>jq -nr '{foo: "bar",baz: "test"}|@sh'
jq: error (at <unknown>): object ({"foo":"bar...) can not be escaped for shell

It would be nice if it would "just work" and output assignments similar to:

>jq -nr '{foo: "bar",baz: "test"}|to_entries|.[]|"\(.key)=\(.value|@sh)"'
foo='bar'
baz='test'
@b0o
Copy link

b0o commented Jul 31, 2019

I actually had this need as well, so I wrote a few helper functions:

def to_entries_recursive(p):
  if isempty(p) then
    {
      root: true,
      type: type,
      path: [],
      value: to_entries_recursive([])
    }
  elif type == "array" or type == "object" | not then . else
    to_entries | map([.value, p + [.key]] as [$val, $path] |
      {
        key: .key,
        type: $val | type,
        $path,
        value: $val | to_entries_recursive($path),
      }
    )
  end
;
def to_entries_recursive: to_entries_recursive(empty);

def flatten_tree:
  def _handle_node:
    if .type == "array" or .type == "object" | not then
      { (.path | join("__")): .value }
    else
      .value | map(_handle_node) | add
    end
  ;
  to_entries_recursive | _handle_node
;

def shell_fmt:
  if type != "object" then
    @sh
  else
    flatten_tree | to_entries | map("\(.key)=\(.value | @sh)")
  end
;

Usage:

jq 'shell_fmt'
   {"foo": 42}
=> ["foo=42"]

jq 'shell_fmt'
   {"name": {"first": "Rob", "last": "Tables", "title": "Sr."}, "kids": ["Roberta", "Bobby"]}
=> ["name__first='Rob'","name__last='Tables'","name__title='Sr.'","kids__0='Roberta'","kids__1='Bobby'"]

jq 'shell_fmt'
   42
=> ["42"]

jq 'shell_fmt'
   [1,3,5,7]
=> ["1 3 5 7"]

jq 'shell_fmt'
   {"age": 99.9, "foo":[1,"bar",{"qux": [1,9,{"key": "huh?", "value": "idk lol"}]}], "name": "bob"}
=> ["age=99.9","foo__0=1","foo__1='bar'","foo__2__qux__0=1","foo__2__qux__1=9","foo__2__qux__2__key='huh?'","foo__2__qux__2__value='idk lol'","name='bob'"]

It's not perfect:

  • arrays within objects are not represented the same as arrays on their own:

    jq 'shell_fmt'
       [1,3,5,7]
    => ["1 3 5 7"]
    
    jq 'shell_fmt'
       {"arr": [1,3,5,7]}
    => ["arr__0=1","arr__1=3","arr__2=5","arr__3=7"]
    
  • doesn't sanitize object keys:

    jq 'shell_fmt'
       '{"tell it like it is!": "ok sure thing"}'
    => ["tell it like it is!='ok sure thing'"]
    
  • due to the way keys/indices are serialized with __ as a separator, an object's key can "overwrite" other ones:

    jq 'shell_fmt'
       {"a": [1,2,3], "b": {"aay": "yo"}, "a__0": -42, "b__aay": "naw"}
    => ["a__0=-42","a__1=2","a__2=3","b__aay='naw'"]
    
  • probably more issues

If you trust the source of your json and know reasonably well its structure won't have these issues, this should work in a pinch. With some more work, maybe it could be made semi-robust?

Otherwise, I don't recommend it for anything important, but maybe it can give you some ideas!

@garthk
Copy link

garthk commented Aug 5, 2023

See also #789?

nicowilliams added a commit to nicowilliams/jq that referenced this issue Aug 6, 2023
nicowilliams added a commit to nicowilliams/jq that referenced this issue Aug 6, 2023
nicowilliams added a commit to nicowilliams/jq that referenced this issue Aug 6, 2023
nicowilliams added a commit to nicowilliams/jq that referenced this issue Aug 7, 2023
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

4 participants