3 ms·
In the linked github wiki there's an example of the syntax of the suggested --jp flag used to pass key-value pairs and put them together as a JSON object:[1] -
by hnarn 5y ago
In the linked github wiki there's an example of the syntax of the suggested --jp flag used to pass key-value pairs and put them together as a JSON object:[1]
--jp a=b --jp c=d --jp e=2 --jp f=false
Gives:
{
"a": "b",
"c": "d",
"e": 2,
"f": false
}
--jp map=europe --jp prime[]=13 --jp prime[]=17 --jp target[x]=-10 --jp target[y]=32
Gives:
{
"map": "europe",
"prime": [
13,
17
],
"target": {
"x": -10,
"y": 32
}
}
While this is neat, I suppose, it seems like such a waste that the first one isn't given as:
--jp a=b,c=d,e=2,f=false
And the second as:
--jp map=europe --jp prime[]=13,17 --jp target[]=x:-10,y:32
...or similar. The repetition kind of bothers me.
[1]: https://github.com/curl/curl/wiki/JSON https://github.com/curl/curl/wiki/JSON
- jffry 5y agoThen you wouldn't be able to reliably use shell substitution to pass in values, which is a pity. The issue being if $var contained commas, like var="foo,y=bar" curl --jp "x=$var" Then allowing comma-separated field=value pairs within a single --jp argument would cause non-obvious changing behavior
- hnarn 5y agoYou could possibly solve it by supporting single quotes for value containment: curl --jp "x='$var'" Gives: curl --jp "x='foo,y=bar'" Assuming that is what you meant. Isn’t this normally how these things are handled in unix shells? That and escaping which probably doesn’t apply here. I don’t know what the best way to implement this would be, but the current proposal looks so weird for me that I’m either completely missing something or it’s wildly unnecessary.
- jffry 5y agoIf you do something like single quotes then you can no longer use the shell's abilities to properly pass in escaped values like this: bash$ foo=this\"test\" bash$ echo "value of \$foo is: $foo" value of $foo is: this"test" If we're throwing away the ability to let the shell handle escaping properly then there's really no point to --jq at all versus just manually attempting to cobble together JSON directly