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

Suberflous \ if try to escape } in value #187

Closed
hikhvar opened this issue Sep 14, 2022 · 8 comments
Closed

Suberflous \ if try to escape } in value #187

hikhvar opened this issue Sep 14, 2022 · 8 comments

Comments

@hikhvar
Copy link

hikhvar commented Sep 14, 2022

Hello,

I want to encode some JSON as string value. Therefore I used the trick to escape both the leading { and the trailing } with an \ like this:

jo foo='\{"bla":"blub"\}'
{"foo":"{\"bla\":\"blub\"\\}"

 jo foo='\{"bla":"blub"\}' -p | jq .foo
"{\"bla\":\"blub\"\\}"

As you can see, jo adds two \\ for the trailing }

My expected result would have been:

 jo foo='\{"bla":"blub"\}' -p | jq .foo
"{\"bla\":\"blub\"}"

If only the leading { is escaped, jo detects the value as nested object:

jo foo='\{"bla":"blub"}' -p
{
   "foo": {
      "bla": "blub"
   }
}

Jo Version:

jo -v
jo 1.6
@jpmens
Copy link
Owner

jpmens commented Sep 14, 2022

You are not showing us exactly what you get (e.g the diagnosting warning):

$ jo foo='\{"bla":"blub"\}' -p
Argument `-p' is neither k=v nor k@v
{"foo":"{\"bla\":\"blub\"\\}"}

This is probably not a bug but simply an esacaping thing. I think you'll find this solves your task:

$ jo bla=blub > /tmp/f
$ jo  foo=@/tmp/f | jq .
{
  "foo": "{\"bla\":\"blub\"}"
}

@jpmens jpmens closed this as completed Sep 14, 2022
@gromgit
Copy link
Collaborator

gromgit commented Sep 14, 2022

jo foo='\{"bla":"blub"\}'

jo can just as easily do that for you in a sane way, with nested invocations and (very VERY important) proper quoting of each value:

$ jo -p foo="$(jo bla=blub)"
{
   "foo": {
      "bla": "blub"
   }
}

$ jo -p foo="$(jo bar="$(jo baz="$(jo -a 42 78 hike)")" flurb=tens)"
{
   "foo": {
      "bar": {
         "baz": [
            42,
            78,
            "hike"
         ]
      },
      "flurb": "tens"
   }
}

@jpmens
Copy link
Owner

jpmens commented Sep 14, 2022

@gromgit quite right, but I'm understanding OP doesn't want the nested object but rather that foo contains the string {"bla":"blub"}.

@hikhvar
Copy link
Author

hikhvar commented Sep 14, 2022

You are not showing us exactly what you get (e.g the diagnosting warning):

That is not true, maybe my build (binary package from Archlinux) is broken, but I don't get any diagnostics warning here.

jo

@gromgit as @jpmens said, I want to have the string instead of the nested object.

@jpmens I don't understand why this odd escaping behaviour isn't a bug? I double checked it with both zsh and bash, both of the time the escaping seems to get broken within jo.

@gromgit
Copy link
Collaborator

gromgit commented Sep 14, 2022

Sorry, I misunderstood. Still, assuming you want {"bla": "blub"} to be treated as a string, jo has allowed string coercions for a long time now:

$ jo -p -- -s foo='{"bla": "blub"}'
{
   "foo": "{\"bla\": \"blub\"}"
}

 jo -p -- -s foo="$(jo bla=blub)"
{
   "foo": "{\"bla\":\"blub\"}"
}

Note that the first example actually preserved the space after the colon, proving conclusively that the value was indeed treated as a string instead of being interpreted by jo.

@jpmens
Copy link
Owner

jpmens commented Sep 14, 2022

I invented jo, but @gromgit remains the person who knows it best. Thank you, Adrian.

@hikhvar
Copy link
Author

hikhvar commented Sep 14, 2022

ok, sorry, I tried with jo -s foo='{"bla": "blub"}' first, but only got the message

jo: invalid option -- 's'
[...help...]

Should have read the manual more in depth.

@TimDeve
Copy link

TimDeve commented Oct 1, 2022

Sorry for the comment on a closed issue but I thought it might be helpful to people finding this issue.
A more streamline way is to use the shell "process substituion" or <():

jo foo=@<(jo bla=blu)

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