stefankoegl / python-json-pointer

Resolve JSON Pointers in Python
https://python-json-pointer.readthedocs.org/
Other
140 stars 43 forks source link

jsonpointer commandline utility: allow passing pointers as args and read stdin #25

Closed eadmaster closed 6 years ago

eadmaster commented 6 years ago

e.g.: jsonpointer "/b" a.json

Also would be useful if it could act as a unix filter for pretty-printing json files and filtering via json pointers. e.g.: cat a.json | jsonpointer "/b"

stefankoegl commented 6 years ago

Currently the commandline utility expects 2+ arguments, and all of them are expected to be filenames. I agree that a pointer string seems to be more intuitive than a file containing a pointer, but changing that would break backwards compatibility.

I am not sure which option would be better

eadmaster commented 6 years ago

You can just check if the 1st arg exists as a file and if not assume it is a json pointer. This is a bit less robust but won't break backward compatibility.

stefankoegl commented 6 years ago

That opens a lot of issues... What if it exists, but is not readable? It would cause a read for every call, unnecessary if your use case is intended. What if you want to pass a pointer that also exists as file? The same input on two different systems might return different results (based on the file system).

I'd rather be able to distinguish solely based on the call.

eadmaster commented 6 years ago

then go with option 2 and break backward compatibility.

If you want to read the pointer from a file you can also do this without any extra switch: jsonpointer "$(cat ptr.json)" a.json

eadmaster commented 6 years ago

I ended up making my own tool in python: http://eadmaster.altervista.org/pub/prj/cliapps/jp.py (still sketchy, but it works)

xenoterracide commented 6 years ago

I too would say break backwards compatibility, or release another "binary". I'm not really opposed to options, but if you do that you may want to add those for reading files as well and then deprecate the default.

 autoscaling describe-auto-scaling-instances --region us-east-1 | jsonpointer "/AutoScalingInstances/1"

otherwise I actually have to write it to a file first

ryanmorillo commented 6 years ago

Here is a wrapper (make sure you chmod +x it first): cat pipepointer.sh

!/bin/bash

mkfifo something while IFS=EOF read -r line do echo $line > something & /usr/local/bin/jsonpointer $1 something done rm something

usage: cat some.json | ./pipepoionter.sh ptr.json

It will read the ptr.json and use a named pipe to read in the piped data. (Only works on POSIX systems, because bash and I don't know how to do a named pipe on windows, but msdn says they exist) Bash should be able to be susbstitued for the shell of your choice, may need mknod instead of mkfifo for other shells. Not sure of the particulars.

xenoterracide commented 6 years ago

tangential, but in the event anyone finds this, but needs something more powerful, jq is an alternative, though I have to install it on centos

 aws autoscaling describe-auto-scaling-groups |jq '.AutoScalingGroups[]| select( .Tags[].Value == "playground").Instances[].InstanceId'
stefankoegl commented 6 years ago

I have implemented this in a separate branch now (see #26). For testing, you can download the commandline utility from

https://raw.githubusercontent.com/stefankoegl/python-json-pointer/0fb516fa310db0eb4900a7b529c64bde290390bb/bin/jsonpointer

The updated version accepts the following

jsonpointer <pointer-str> <json-files>
jsonpointer -f <pointer-file> <json-files>

The json-files can also be - to read from stdin (this already works in the current version).

Please let me know if that would match your requirements. If so, I'll merge and probably release as 2.0 as it breaks compatibility of the commandline utility.