Skip to content

Commit ad50cc2

Browse files
committed
Initial commit
Signed-off-by: Bill Maxwell <[email protected]>
0 parents  commit ad50cc2

File tree

6 files changed

+1382
-0
lines changed

6 files changed

+1382
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/node_modules

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# gptscript JSON query tool
2+
3+
This tool is a simple command line tool to query json documents using jq. It is written in javascript and uses the node-jq package to install and run jq.
4+
5+
## Installation
6+
7+
```bash
8+
git clone github.com/gptscript/json-query
9+
cd json-query
10+
npm install
11+
```
12+
13+
## CLI Usage
14+
15+
To test this command out directly you can run:
16+
17+
```bash
18+
node jq.js --query '.' --jsonpath ./package.json
19+
```
20+
21+
this will output the local package.json file.
22+
23+
## gptscript usage
24+
25+
To use this tool in a gptscript you can use the following code:
26+
27+
```gptscript
28+
tools: ./tool.gpt
29+
30+
What is the version specified in the package.json file
31+
```
32+
33+
A more complex example:
34+
35+
```gptscript
36+
tools: ./tool.gpt,sys.download
37+
38+
download https://raw.githubusercontent.com/bahamas10/css-color-names/master/css-color-names.json
39+
find out the css values for darkviolet and the color blue
40+
```

jq.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const jq = require('node-jq');
2+
// Simple argument parsing function
3+
function parseArgs() {
4+
const args = {};
5+
process.argv.slice(2).forEach((val, index, array) => {
6+
if (val.startsWith('--')) {
7+
const argName = val.substring(2);
8+
// Check if we are not at the last element and the next element doesn't start with '--'
9+
if (index < array.length - 1 && !array[index + 1].startsWith('--')) {
10+
args[argName] = array[index + 1];
11+
}
12+
}
13+
});
14+
return args;
15+
}
16+
17+
const args = parseArgs();
18+
19+
// Fallback to default values if arguments not provided
20+
const filter = args.filter || '.';
21+
const jsonPath = args.jsonpath;
22+
const options = {
23+
input: 'file',
24+
output: 'json'
25+
};
26+
27+
jq.run(filter, jsonPath, options)
28+
.then((output) => {
29+
console.log(output);
30+
})
31+
.catch((err) => {
32+
console.error(err);
33+
});

0 commit comments

Comments
 (0)