Comments (8)
You can use gojq.WithFunction
to add a custom internal function. You can invoke shell command if you like to within your function implementation.
from gojq.
@noperator here is an example of a function that runs a shell command #101 (comment) but as usual with shell and exec, be careful
from gojq.
Thanks. Also appreciate you closed it, I would rather a repo I use have few open issues than a bunch of out of scope feature requests. I'll blog if I do anything interesting with my newfound knowledge of WithFunction!
from gojq.
Hey @benatkin, did you end up using WithFunction
to filter values through a shell command? Would love an example if you have one.
from gojq.
That's perfect, thanks @wader. @lgfausak suggested in that same issue that one could:
compile in a new module which I have access to as a function from jq
If I understand that comment correctly, then it seems like I could:
- Use
gojq
to compile a module providing a custom function (e.g.,cmd()
) - Write that module to disk at
~/cmd.jq
- Import that module into
jq
to invoke the custom function, likejq -L ~/cmd.jq '"hello" | cmd(["md5"])'
I'm going to play around with that to test my understanding of this whole process. If you happen to see any obvious gaps in that approach, please let me know.
from gojq.
@noperator Your welcome. There is no support for loading "native" modules as such at runtime, instead you can for example build your own version of the gojq cli tool with that function added.
from gojq.
Loading extensions at runtime is general not easy to achieve with golang? I think if possible it would be very nice to implement new or special features to gojq as an extension in a separate file. To provide a proof-of-concept or try out something and gather feedback. gojq could stay close to upstream. Maybe extensions could be namespaced by default, so no collisions with future jq changes possible.
from gojq.
That is my understanding also but i have no experience doing it. Read a bit about the plugin
package out of curiosity and it seems like it requires the plugin to be compiled with exact same golang version and GOPATH etc, not very convenient. Other option i guess would be wasm somehow? but in either case gojq package would require to have some kind of stable plugin API i guess and at what level? WithFunction
/WithIterator
or lower level?
Maybe if you want to experiment you could import gojq and build some bridge to WithFunction
/WithIterator
somehow?
Depending on what performance your looking for an alternative could be to mimic something like fastcgi? let an external process get calls as serialized JSON and respond with JSON? using the code i linked to above you can prototype such things like this:
def _process_rpc($cmd): tojson | cmd($cmd) | fromjson;
def json_http($method; $url):
_process_rpc(["curl", "-X", $method, "-H", "Content-Type: application/json", "-d", "@-", "-o", "-", $url]);
{bla: 123} | json_http("GET"; "https://httpbin.org/anything")
{
"args": {},
"data": "{\"bla\":123}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "11",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.84.0",
"X-Amzn-Trace-Id": "Root=1-63669f6d-17a435df1c8948ba322b588c"
},
"json": {
"bla": 123
},
"method": "GET",
"origin": "...",
"url": "https://httpbin.org/anything"
}
from gojq.
Related Issues (20)
- Ability to concurrently run many queries on a single JSON object
- Duplicate keys in object literals have inconsistent behavior HOT 4
- Curious bug when reading from /dev/urandom or /dev/random HOT 2
- Allocated maps are ignored
- Support strings with JSON as input HOT 3
- `halt/0` (& co.) have the old jq 1.6 behaviour HOT 1
- Example of retrieving JSON or unmarshalled object HOT 4
- error parsing regexp: invalid named capture HOT 2
- Tests fail for s390x in Fedora since 0.12.15 HOT 3
- Question about handling NDJSON with the library HOT 1
- Week number is not taken into account during date creation in `strptime` HOT 5
- error parsing regexp that contains "?<=" HOT 3
- Add files/line/column to run errors like iteratorError?
- Possible memory leak from calling functions HOT 3
- Using "/" in a string constant HOT 2
- Question on the use of "as" in v0.12.16 HOT 3
- Make available via conda-forge HOT 3
- Support 1.23 iterator HOT 1
- Jo HOT 1
- error-handling discrepancy when reading mal-formed JSON
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gojq.