https://github.com/pichsenmeister/json-keys
Small libary to retrieve keys and their paths of a JSON object or array.
https://github.com/pichsenmeister/json-keys
json json-filter json-keys json-schema json-tools
Last synced: 29 days ago
JSON representation
Small libary to retrieve keys and their paths of a JSON object or array.
- Host: GitHub
- URL: https://github.com/pichsenmeister/json-keys
- Owner: pichsenmeister
- License: mit
- Created: 2020-05-18T01:59:35.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-20T19:50:01.000Z (almost 3 years ago)
- Last Synced: 2025-10-10T10:41:26.174Z (8 months ago)
- Topics: json, json-filter, json-keys, json-schema, json-tools
- Language: JavaScript
- Homepage:
- Size: 2.57 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-keys
Small libary to retrieve keys of a JSON object or array.
* [Installation](#installation)
* [Getting started](#getting-started)
* [License](#license)
## Installation
Install via npm
```
npm install @barreljs/json-keys
```
or yarn
```
yarn add @barreljs/json-keys
```
## Getting started
```
const JsonKeys = require('@barreljs/json-keys')
```
### `.keys(json)`
Returns all keys as an array of strings
#### Example
```
const keys = JsonKeys.keys({
key: 'value',
obj: {
key: 'value'
}
})
```
returns
```
['key', 'obj', 'key']
```
### `.uniqueKeys(json)`
Returns all unique keys as an array of strings
#### Example
```
const keys = JsonKeys.uniqueKeys({
key: 'value',
obj: {
key: 'value'
}
})
```
returns
```
['key', 'obj']
```
### `.paths(json)`
Returns all keys including their path in dot notation as an array of strings
#### Example
```
const paths = JsonKeys.paths({
key: 'value',
obj: {
key: 'value'
},
arr: [
{
key: 'value'
}
]
})
```
returns
```
['key', 'obj', 'obj.key', 'arr', 'arr.0', 'arr.0.key']
```
### `.hasKey(json, key)`
checks if a path (or sub-path) in dot notation is part of a JSON object.
#### Example
```
// returns true
JsonKeys.hasKey({
key: 'value',
obj: {
key: 'value'
},
arr: [
{
key: 'value'
}
]
}, '0.key')
// returns false
JsonKeys.hasKey({
key: 'value',
obj: {
key: 'value'
},
arr: [
{
key: 'value'
}
]
}, 'tree.doesNotExist')
```
## License
This project is licensed under the MIT license, Copyright (c) 2020 David Pichsenmeister | [pichsenmeister.com](https://pichsenmeister.com). For more information see [LICENSE](LICENSE).