Is it possible to load JSON data from STDIN while specifying code with --exec?

539 views
Skip to first unread message

Nikos Mouchtaris

unread,
Aug 8, 2020, 12:24:59 PM8/8/20
to Jsonnet
Hello,

I am trying to edit "inline" JSON streams, for instance as a part of a shell pipe line.

For those familiar, I am trying to replicate the functionality of JQ. In summary, this is having the ability to load JSON data from STDIN, while specifying a "transformation" as part of the command line arguments.

I have not found a way to use STDIN as data directly, because using STDIN negates the --exec argument.

This scenario forces the user to retreat to very weird shell tricks (store a whole JSON document in a variable so that it can be then passed as a --tla-* argument to the --exec program, or store it to intermediate files). I think this is awkward and non-scalable.

Is there something I am missing about this usage scenario?

Thank you,
Nikos

Michael Lang

unread,
Aug 8, 2020, 4:09:23 PM8/8/20
to Nikos Mouchtaris, Jsonnet
It may kind of also be a 'shell trick' but you can achieve this by importing the special file /dev/stdin (at least on typical *nix systems):

$ jsonnet -e '(import "/dev/stdin") + {bar: 1}'
{"foo": 0}
^D
{
   "bar": 1,
   "foo": 0
}

Nikos Mouchtaris

unread,
Aug 10, 2020, 5:46:34 AM8/10/20
to Jsonnet
Thank you Michael, those tricks are better than the ones I thought.

Dave Cunningham

unread,
Aug 10, 2020, 7:39:44 AM8/10/20
to Nikos Mouchtaris, Jsonnet
There is indeed a kernel limit on how much data can be passed via other mechanisms, but importing /dev/stdin does work quite well.

--
You received this message because you are subscribed to the Google Groups "Jsonnet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonnet+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jsonnet/b3b453de-a04e-4ca8-81ef-219d804d8179o%40googlegroups.com.

Brett Viren

unread,
Aug 10, 2020, 8:14:26 AM8/10/20
to Michael Lang, Nikos Mouchtaris, Jsonnet
Here is a variation on a theme. This keeps "/dev/stdin" out of the
Jsonnet code but at the expense of requiring a top level function:

$ echo '{a:42}' | jsonnet --tla-code-file x=/dev/stdin -e 'function(x) x'
{
"a": 42
}

-Brett.
> --
> You received this message because you are subscribed to the Google Groups
> "Jsonnet" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jsonnet+u...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jsonnet/CACpAYXXjjcKsgDYj4prBdd8gfVkdg4c3O8Fip_Q0MC_pmX%2BC9w%40mail.gmail.com
> .
signature.asc
Reply all
Reply to author
Forward
0 new messages