# json-power-query

> A simpler version of a json querying mechanism.

Latest version **1.1.21** (published 2024-11-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-power-query
pnpm add json-power-query
yarn add json-power-query
bun add json-power-query
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.21 |
| Published | 2024-11-26 |
| First published | 2021-05-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 67.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jesse Daniel Mitchell |
| Maintainers | totaltechgeek |

## Links

- npm: https://www.npmjs.com/package/json-power-query
- Repository: https://github.com/TotalTechGeek/json-power-query
- npm.io page: https://npm.io/package/json-power-query

## Dependencies (1)

- [json-logic-engine](https://npm.io/package/json-logic-engine.md) ^3.0.2

## Recent versions

- 1.1.21 (latest) — 2024-11-26
- 1.1.20 — 2023-07-24
- 1.1.19 — 2022-04-12
- 1.1.18 — 2022-03-24
- 1.1.17 — 2022-03-03
- 1.1.16 — 2022-02-16
- 1.1.15 — 2021-11-02
- 1.1.14 — 2021-10-30
- 1.1.13 — 2021-10-26
- 1.1.12 — 2021-10-26
- 1.1.11 — 2021-10-25
- 1.1.10 — 2021-10-25
- 1.1.9 — 2021-10-25
- 1.1.8 — 2021-08-27
- 1.1.7 — 2021-08-22
- … 10 more at https://npm.io/package/json-power-query/versions

## README

# JSON Power Query

This is a simple JSON querying mechanism similar to jsonpath. This mechanism allows for ultra-fast queries by "compiling" your queries into evaluated JavaScript.

At the same time, the mechanism uses `json-logic-engine` to power its filtering, to prevent the unsafe evaluation of any JavaScript code.

### Examples 

Given the following: 
```js
const request = {
    body: {
        name: "John Doe",
        age: 35,
        friends: [
            {
                name: "Steve",
                age: 23
            },
            {
                name: "Bob",
                age: 65
            },
            {
                name: "Erik",
                age: 33
            }
        ]
    },
    params: {
        id: 101
    }
}
```


Requesting a single property: 
```js
const { queryBuilder, objectQueryBuilder } = require('json-query-engine')
const nameGetter = queryBuilder('$.body.name')
console.log(nameGetter(request)) // prints: John Doe
```


Requesting nested properties from an array: 
```js
const friendsNames = queryBuilder('$.body.friends.*.name')
console.log(JSON.stringify(friendsNames(request))) // prints: ["Steve", "Bob", "Erik"]
```

Requesting filtered values from an array: 
```js
// the filtering uses json-logic-engine, and uses truthiness.
const olderFriendNames = queryBuilder('$.body.friends.*{ ">": [{ "var": "age" }, 30] }.name')
console.log(JSON.stringify(olderFriendNames(request))) // prints: ["Bob", "Erik"]
```

Alternatively, you can use the more normal looking JSONPath Syntax:
```js
const olderFriendNames = queryBuilder('$.body.friends.[?(@.age > 30)].name')
console.log(JSON.stringify(olderFriendNames(request))) // prints: ["Bob", "Erik"]
```


You can also build a function that transforms object shapes into different shapes:
```js
const f = objectQueryBuilder({
    name: '$.body.name',
    friends: '$.body.friends.*.name',
    id: '$.params.id',
    meta: {
        friendAges: '$.body.friends.*.age'
    }
})

const y = f(request)  
/* Generates:
 * { 
 *     "name": "John Doe", 
 *     "friends": [ 
 *         "Steve", 
 *         "Bob", 
 *         "Erik" 
 *     ], 
 *     "id": 101, 
 *     "meta": { 
 *         "friendAges": [ 
 *             23, 
 *             65, 
 *             33 
 *         ] 
 *     } 
 * }
 */ 

```

---
_Source: https://npm.io/package/json-power-query · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
