https://github.com/lorefnon/hbs-jsonpath-helper
A simple handlebars helper that enables you to use jsonpath (to access and traverse nested data) within your templates
https://github.com/lorefnon/hbs-jsonpath-helper
Last synced: about 2 months ago
JSON representation
A simple handlebars helper that enables you to use jsonpath (to access and traverse nested data) within your templates
- Host: GitHub
- URL: https://github.com/lorefnon/hbs-jsonpath-helper
- Owner: lorefnon
- Created: 2019-08-02T02:55:48.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-10-18T04:10:09.000Z (over 2 years ago)
- Last Synced: 2025-05-10T16:11:55.633Z (about 1 year ago)
- Language: TypeScript
- Size: 441 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
README
# hbs-jsonpath-helper
A simple handlebars helper that enables you to use jsonpath (to access and traverse nested data) within your templates
## Resources:
- Baeldung's [Introduction to jsonpath](https://www.baeldung.com/guide-to-jayway-jsonpath)
- Online [jsonpath evaluator](https://jsonpath.com/)
- [jsonpath-plus](https://www.npmjs.com/package/jsonpath-plus) npm package (which the helpers defined here delegate to)
## Installation & Usage
```
npm install --save handlebars hbs-jsonpath-helper
```
```js
const Handlebars = require('handlebars');
const jsonPathHelper = require('hbs-jsonpath-helper');
jsonPathHelper.register();
// Or, you can explicitly pass a Handlebars instance
jsonPathHelper.register(Handlebars);
// Compile template
const template = Handlebars.compile(`
{{jp-get "$.store.book[0].title"}}
`);
// Data passed to the template
const data = { /* ... */ }
// Use compiled template:
const str = template(data); // "Nigel Rees"
```
## API & Examples
For the following data source
```
const data = {
store: {
book: [
{
category: 'reference',
author: 'Nigel Rees',
title: 'Sayings of the Century',
price: 8.95,
},
{
category: 'fiction',
author: 'Evelyn Waugh',
title: 'Sword of Honour',
price: 12.99,
},
{
category: 'fiction',
author: 'Herman Melville',
title: 'Moby Dick',
isbn: '0-553-21311-3',
price: 8.99,
},
{
category: 'fiction',
author: 'J. R. R. Tolkien',
title: 'The Lord of the Rings',
isbn: '0-395-19395-8',
price: 22.99,
},
],
bicycle: {
color: 'red',
price: 19.95,
},
},
};
```
### jp-get
Retrieve a value from current context using jsonpath
*Template:*
```
Books
-
{{title}}
{{#each (jp-get "$.store.book[*]")}}
{{/each}}
```
*Result:*
```
Books
-
Sayings of the Century
-
Sword of Honour
-
Moby Dick
-
The Lord of the Rings
```
### jp-get-in
Retrieve a value from specified target using jsonpath
*Template:*
```
Books
-
{{title}}
{{#each (jp-get-in "book[*]" store)}}
{{/each}}
```
*Result:*
```
Books
-
Sayings of the Century
-
Sword of Honour
-
Moby Dick
-
The Lord of the Rings
```
### jp-descend
Use the value derived from current context using jsonpath as the context
for embedded block
*Template:*
```
Authors
- {{.}}
{{#jp-descend "$.store.book[*].author"}}
{{#each .}}
{{/each}}
{{/jp-descend}}
```
*Result:*
```
Authors
- Nigel Rees
- Evelyn Waugh
- Herman Melville
- J. R. R. Tolkien
```
### jp-descend-in
Use the value derived from specified target using jsonpath as the context
for embedded block
*Template:*
```
Authors
- {{.}}
{{#jp-descend-in "book[*].author" store}}
{{#each .}}
{{/each}}
{{/jp-descend-in}}
```
*Result:*
```
Authors
- Nigel Rees
- Evelyn Waugh
- Herman Melville
- J. R. R. Tolkien
```
## License
MIT