Live data from Hacker News

Jo – a shell command to create JSON (2016)

jpmens.net

91–100 of 100 posts

Re: Jo – a shell command to create JSON (2016)

#91
post #85
post #82

Earlier quoted context omitted.

jo immediately reminded me of httpie’s CLI syntax. Is it just a coincidence, or does httpie use jo under the hood? Btw loving your new desktop app so far!

We did look at `jo`, and also `jarg`[0], the W3C HTML JSON form syntax[1], and pretty much every other approach we could find. We had quite a few requirements that the new nested syntax had to meet: be simple/flexible, easy to read/write, backward/forward compatible, and play well with the rest of the HTTPie request language. The final syntax is heavily inspired by the HTML JSON forms one. But we made it stricter, ad…

Thanks for the detailed reply! Hadn’t come across the HTML JSON form syntax before. Good to know memorising the httpie request language will have some side benefits in that it’s closely related to a standard in use elsewhere.

Re: Jo – a shell command to create JSON (2016)

#92

Earlier quoted context omitted.

> Even though Python isn't the fastest language out there, it's likely still faster than the shell command above. Taking these two command lines: jo -p name=JP object=$(jo fruit=Orange point=$(jo x=10 y=20) number=17) sunday=false >/dev/null python -c 'import json;print(json.dumps({"name": "JP", "object": {"fruit": "Orange", "point": {"x": 10, "y": 20}, "number": 17}, "sunday": False}))' >/dev/null For jo (x86_64, Ro…

what about how long for a human to read it and debug it when it gets beyond trivial?

Fair question. I think jo does tend to get more crufty if you're doing anything reasonably complex with multilevel structures, especially with arrays.

But jo does come into its own when you're wanting to use shell variables.

    > jo mypid=$$ set_or_not=$WEASEL

    > python -c 'import json,os;print(json.dumps({"set_or_not":os.getenv("WEASEL"), "mypid":os.getpid()}))'

Re: Jo – a shell command to create JSON (2016)

#93

Earlier quoted context omitted.

That wasn't the claim made in the original post though, was it? The claim was that the Python snippet would be quicker than the jo snippet. "Even though Python isn't the fastest language out there, it's likely still faster than the shell command above." Which is most definitely is not - it's 5x slower. (Probably not a huge issue in the real world if you're writing a shell script, mind, given that bash itself isn't a…

That’s because you’re making a false assumption about the environment prior to executing the statement. If you are in a shell session and have to choose between executing python -c or calling jo, the latter is faster as you’ve demonstrated. But that’s not a realistic assumption. Statements like these are almost certainly part of some combined work. The data you’re feeding to jo comes from somewhere. Its output is wri…

> if you’re already inside some Python script [...]

You're not going to shell out to `jo` and that's fine - it's not what `jo` was created for; it's explicitly a shell command to help you work around the annoyance of getting quoting right when constructing JSON from the command line (which I've had to do a lot and I'm pretty sure many people have to.)

> If you are in a shell session [and want to create JSON] ... that’s not a realistic assumption.

Of course it is. People create JSON in shell scripts all the time! That's why things like `jq` exist - because this is what people do!

Re: Jo – a shell command to create JSON (2016)

#94
post #19

>Bam! Jo tries to be clever about types and knows null, booleans, strings and numbers. I'm very skeptical of this. If I put x=001979 in as a value I dont think I want you trying to guess if that's supposed to be an integer or a string. This sounds like the Norway Problem waiting to happen.

It is reminiscent of a feature in YAML that has bitten people. But, clearly there is a use-case for producing json with integer, null, and boolean values.

Here is how HTTPie does it:

  x=005  // {"x": "005"}
  X:=005 // {"x": 5}

Re: Jo – a shell command to create JSON (2016)

#95
post #77

In the common case where you trust your input entirely you can just interpret your string as JavaScript. Then you don't even need to use quotes for the key names. $ alias fooson="node --eval \"console.log(JSON.stringify(eval('(' + process.argv[1] + ')')))\"" $ fooson "{time: $(date +%s), dir: '$HOME'}" {"time":1457195712,"dir":"/Users/jpm"} It may be a bit nicer to place that JavaScript in your path as a node script…

Ended up sniping myself . Made a fairly complete version of what I was imagining: https://github.com/itsjohncs/construct-json#readme

Re: Jo – a shell command to create JSON (2016)

#96

I remember that when I worked at Google about a decade ago, there was this common saying: "If the first version of your shell script is more than five lines long, you should have written it in Python." I think there's a lot of truth in that. None of the examples presented in the article look better than had they been written in some existing scripting/programming language. In fact, had they been written in Python or…

Often when writing scripts, I'm chaining tools together, e.g. using git to find a thing in a specific commit, using curl to grab something from the web, decoding some json, maybe unzipping a file. I've never really found any language that feels good for that kind of thing, there's definately a middle ground where it's getting too much for bash, but jumping to a language loses too much at the initial conversion to mak…

Thank you for pointing out that jq can create JSON! I use jq all the time for working with the AWS CLI and a big pain point has always been sending JSON. If you don't know, the AWS CLI depends on JSON arguments for quite a few common tasks, and the JSON needed can be quite lengthy.

Up until now I've been creating temp JSON files to feed the commands and I thought jo would be a great tool to make this easier. Now that I know jq can also create JSON, I'll just use that instead.

Re: Jo – a shell command to create JSON (2016)

#97

Dang yo, why it's not " Jo – a shell command to create JSON written in C (2016) (jpmens.net)" You guys do that for "written in Rust"

The original title does not contain “written in C”. Do moderators usually rewrite the title to add things like “written in Rust”?

Re: Jo – a shell command to create JSON (2016)

#98

I remember that when I worked at Google about a decade ago, there was this common saying: "If the first version of your shell script is more than five lines long, you should have written it in Python." I think there's a lot of truth in that. None of the examples presented in the article look better than had they been written in some existing scripting/programming language. In fact, had they been written in Python or…

> Even though Python isn't the fastest language out there, it's likely still faster than the shell command above. Taking these two command lines: jo -p name=JP object=$(jo fruit=Orange point=$(jo x=10 y=20) number=17) sunday=false >/dev/null python -c 'import json;print(json.dumps({"name": "JP", "object": {"fruit": "Orange", "point": {"x": 10, "y": 20}, "number": 17}, "sunday": False}))' >/dev/null For jo (x86_64, Ro…

I get 14,823s for python3 and 4,667s for jo on my system.

I also wrote my own tool, xidel [1]:

    time for i in $(seq 1 $count); do xidel -se '{"name": "JP", "object": {"fruit": "Orange", "point": {"x": 10, "y": 20}, "number": 17}, "sunday": false()}' > /dev/null; done     

    
which gives me 1,575s

But if you actually want to repeat something a thousand times, you would use a loop in the query for 0,017s:

    time xidel -se 'for $i in 1 to 1000 return {"name": "JP", "object": {"fruit": "Orange", "point": {"x": 10, "y": 20}, "number": 17}, "sunday": false()}'  > /dev/null
  
  
(a python3 loop gives me 0,029s)

[1] https://videlibri.de/xidel.html

Re: Jo – a shell command to create JSON (2016)

#99

Earlier quoted context omitted.

This is a toxic comment that adds nothing to the conversation.

I don't know, the jo command seems a lot more readable to me than compressing json to a one line string. How is it toxic to call out a silly truthism?

I agree with Galanwe, but

> [...] are often the mark of wannabe guru mid career engineers that have no clue what they are talking about.

is the "toxic" part.

At least he says "are often," as not to accuse the anonymous mid career Google engineer.

Re: Jo – a shell command to create JSON (2016)

#100

Five minute job: $ ./jo foo=1 bar=2 obj=$(./jo -a 1 2 3 " def > ghi' {"foo":"abc\ndef\nghi"} $ cat jo #!/usr/local/bin/txr --lisp (define-option-struct jo-opts nil (a array :bool "Produce array instead of object") (nil help :bool "Print this help")) (defvarl jo-name *load-path*) (defun json-val (str) (match-case str ("true" t) ("false" nil) ("null" 'null) (`{@nil` (get-json str)) (`[@nil` (get-json str)) (@else (ifle…

Never heard of txr. Thanks for this.

For anyone else wondering: https://www.nongnu.org/txr/

Post reply on HN