Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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: 1 day ago
JSON representation

Small libary to retrieve keys and their paths of a JSON object or array.

Awesome Lists containing this project

README

        

# json-keys


Dependency Status


devDependency Status

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).