Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mainak55512/jsonprocessor
Simple JSON query tool for JS
https://github.com/mainak55512/jsonprocessor
cli javascript jq json library linux npm-package open-source query sql
Last synced: 16 days ago
JSON representation
Simple JSON query tool for JS
- Host: GitHub
- URL: https://github.com/mainak55512/jsonprocessor
- Owner: mainak55512
- Created: 2023-10-29T19:05:32.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-30T17:46:49.000Z (4 months ago)
- Last Synced: 2024-11-16T08:09:09.603Z (3 months ago)
- Topics: cli, javascript, jq, json, library, linux, npm-package, open-source, query, sql
- Language: JavaScript
- Homepage:
- Size: 87.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Jproc
Jproc is a light-weight simple json array query tool written in JavaScript.
## Installation
```
npm install jproc
```
or,
```
npm i jproc
```## New jproc CLI is now available
πNew 'jproc' cli released.π
Try out new 'jproc' cli tool available in [releases](https://github.com/mainak55512/JSONProcessor/releases).Check out [Jproc-cli-usage](#Jproc-cli-usage) section for more details
## Sample data.json```json
[
{
"name": "name_1",
"age": 18,
"details": {
"id": "hvnakds2342349",
"location": "London",
"company": "xyz",
"keywords": [
"test1",
"test2",
"test3"
]
}
},
{
"name": "name_2",
"age": 20,
"details": {
"id": "iuefibe8362873287",
"location": "London",
"company": "abc",
"keywords": [
"test1",
"test2",
"test3"
]
}
},
{
"name": "name_3",
"age": 19,
"details": {
"id": "iwhiuvwi766579",
"location": "New York",
"company": "xyz",
"keywords": [
"test1",
"test2",
"test3"
]
}
},
{
"name": "name_4",
"age": 25,
"details": {
"id": "wuwcuwey652362",
"location": "Iraq",
"company": "pqr",
"keywords": [
"test1",
"test2",
"test3"
]
}
}
]```
## Example usage:
```javascript
import Jproc from "jproc";
import fs from "fs";let data = JSON.parse(fs.readFileSync("./data.json"));
// Initialize Jproc object with json data.
let jObj = new Jproc(data);//------------------------------------ Example 1 ----------------------------------------
// Optionally you can set which parameters you want in the resultant array form the original json object.
jObj.neededParams(["name", "details.id"]);// Set query for the jproc object, if no query is set using setQuery() method, then it will return resultant array
// with the fields specified by neededParams() method. If no parameter or query specified, then it will throw error.
jObj.setQuery("(age >= 20 || (age > 18 && details.company = 'xyz')) && (details.location = 'London' || details.location = 'New York')");// Finally call exec_query() method to execute the query.
console.log(JSON.stringify(jObj.exec_query()));/*
It matches the condition and returns the fields specified in the neededParams() method called before.
output:
--------
[
{
"name": "name_2",
"id": "iuefibe8362873287"
},
{
"name": "name_3",
"id": "iwhiuvwi766579"
}
]
*/
// ----------------------------------- End of Example 1 -------------------------------------// -------------------------------------- Example 2 -----------------------------------------
// use clearParams() method to clear the parameters set by the neededParams() method.
jObj.clearParams();console.log(JSON.stringify(jObj.exec_query()));
/*
It will match the condition and return all the fields form the json object as we have cleared the parameters set for the jproc object.
Output:
-------
[
{
"name": "name_2",
"age": 20,
"details": {
"id": "iuefibe8362873287",
"location": "London",
"company": "abc",
"keywords": [
"test1",
"test2",
"test3"
]
}
},
{
"name": "name_3",
"age": 19,
"details": {
"id": "iwhiuvwi766579",
"location": "New York",
"company": "xyz",
"keywords": [
"test1",
"test2",
"test3"
]
}
}
]
*/
// ------------------------------- End of Example 2 ---------------------------------// ------------------------------- Example 3 ----------------------------------------
jObj.neededParams(["name", "details.id", "details.location"]);
// Use clearQuery() method to clear existing query for the jproc object.
jObj.clearQuery();console.log(JSON.stringify(jObj.exec_query()));
/*
It doesn't match the condition as we have cleared the query and returns the fields form all the json elements for the array
that are specified in the neededParams() method before.
Output:
-------
[
{
"name": "name_1",
"id": "hvnakds2342349",
"location": "London"
},
{
"name": "name_2",
"id": "iuefibe8362873287",
"location": "London"
},
{
"name": "name_3",
"id": "iwhiuvwi766579",
"location": "New York"
},
{
"name": "name_4",
"id": "wuwcuwey652362",
"location": "Iraq"
}
]
*/
// ------------------------------ End of Example 3 ---------------------------------// ------------------------------ Example 4 ----------------------------------------
jObj.clearQuery();
jObj.clearParams();console.log(JSON.stringify(jObj.exec_query()));
/*
This will throw error as we have cleared both parameters and query.
*/
// ------------------------------ End of Example 4 ---------------------------------```
## Jproc-cli-usage
Jproc cli can read from a file using `--file` flag
```bash
jproc --file="test.json" --query="(age >= 20 || (age > 18 && details.company = 'xyz')) && (details.location = 'London' || details.location = 'New Yo
rk')"
```
Output:
```bash
[
{
name: 'name_2',
age: 20,
details: {
id: 'iuefibe8362873287',
location: 'London',
company: 'abc',
keywords: [Array]
}
},
{
name: 'name_3',
age: 19,
details: {
id: 'iwhiuvwi766579',
location: 'New York',
company: 'xyz',
keywords: [Array]
}
}
]
```Alternatively, json outputs from other applications can be piped to jproc
```bash
stto --json redis | jproc --params="ext","code","file_count" --query="line_count > 200 && file_count < 5"
```Output:
```bash
[
{ ext: 'markdown', code: 176, file_count: 1 },
{ ext: 'm4', code: 669, file_count: 1 },
{ ext: 'vcxproj.filters', code: 446, file_count: 4 },
{ ext: 'vcxproj', code: 1412, file_count: 4 }
]
```