npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

smart-json-templater

v1.0.5

Published

Smart JSON template(r) is an opinionated simple way to do mustache style template replacements (recursively) on your js and json objects (and of course strings too)!.

Downloads

36

Readme

💪 Smart Json Templater 👌

Smart JSON template(r) is an opinionated simple way to do mustache style template replacements (recursively) on your js and json objects (and of course strings too)!.

install

npm i smart-json-templater --save

raw.json (Original data / request)

{
 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,
     },
   ],
   address: 'The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. ',
 },
}

rules.json (Using JSONPath Syntax)

[
  {
    xpath: '$..author', // JSONPath Syntax
    save: 'author', // save the result in this key
  },
  {
    xpath: '$..title', // JSONPath Syntax
    save: 'title', // save the result in this key
  },
  {
    xpath: '$.store.address', // JSONPath Syntax
    save: 'address', // save the result in this key
  },
  {
    xpath: '$.store.book[*].isbn', // JSONPath Syntax
    save: 'isbn', // save the result in this key
  },
  {
    xpath: '$..price',
    save: 'price',
  },
]

JSONPath Syntax

JSONPath | Description -----------------|------------ $ | The root object/element @ | The current object/element . | Child member operator .. | Recursive descendant operator; JSONPath borrows this syntax from E4X * | Wildcard matching all objects/elements regardless their names \[\] | Subscript operator \[,\] | Union operator for alternate names or array indices as a set \[start:end:step\] | Array slice operator borrowed from ES4 / Python ?() | Applies a filter (script) expression via static evaluation () | Script expression via static evaluation

Here are syntax and examples JSONPath.md

template

The template must be a String
const template = '{{~}}' // special key

or a JSON
const template = {
	public: '{{title}} by "{{author}}", only for USD {{price}}',
	library: '{{address}}',
}

Special Keys

Key| Description -----------------|------------ {{~}} | Attach transformed data {{$}} | Attach original data / request

example #1

const templater = require('smart-json-templater')

// example data
const template = '{{~}}'
const rules = require('./rules.json')
const raw = require('./raw.json')

templater.convert(template, rules, raw)
	.then((result)=>{
		console.log(result)
	})

result #1

[
  {
    "author":"Nigel Rees",
    "title":"Sayings of the Century",
    "address":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. ",
    "isbn":null,
    "price":8.95
  },
  {
    "author":"Evelyn Waugh",
    "title":"Sword of Honour",
    "address":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. ",
    "isbn":null,
    "price":12.99
  },
  {
    "author":"Herman Melville",
    "title":"Moby Dick",
    "address":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. ",
    "isbn":"0-553-21311-3",
    "price":8.99
  },
  {
    "author":"J. R. R. Tolkien",
    "title":"The Lord of the Rings",
    "address":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. ",
    "isbn":"0-395-19395-8",
    "price":22.99
  }
]

example #2

const templater = require('smart-json-templater')

// example data
const template = {
	public: '{{title}} by "{{author}}", only for USD {{price}}',
	library: '{{address}}',
}
const rules = require('./rules.json')
const raw = require('./raw.json')

templater.convert(template, rules, raw)
	.then((result)=>{
		console.log(result)
	})

result #2

[
  {
    "public":"Sayings of the Century by \"Nigel Rees\", only for USD 8.95",
    "library":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. "
  },
  {
    "public":"Sword of Honour by \"Evelyn Waugh\", only for USD 12.99",
    "library":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. "
  },
  {
    "public":"Moby Dick by \"Herman Melville\", only for USD 8.99",
    "library":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. "
  },
  {
    "public":"The Lord of the Rings by \"J. R. R. Tolkien\", only for USD 22.99",
    "library":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. "
  }
]

example #3

const templater = require('smart-json-templater')

// example data
const template = {
	type: 'e-book',
	shipping: true,
	available: true,
	information: '{{~}}',
}
const rules = require('./rules.json')
const raw = require('./raw.json')

templater.convert(template, rules, raw)
	.then((result)=>{
		console.log(result)
	})

result #3

[
  {
    "type":"e-book",
    "shipping":true,
    "available":true,
    "information":{
      "author":"Nigel Rees",
      "title":"Sayings of the Century",
      "address":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. ",
      "isbn":null,
      "price":8.95
    }
  },
  {
    "type":"e-book",
    "shipping":true,
    "available":true,
    "information":{
      "author":"Evelyn Waugh",
      "title":"Sword of Honour",
      "address":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. ",
      "isbn":null,
      "price":12.99
    }
  },
  {
    "type":"e-book",
    "shipping":true,
    "available":true,
    "information":{
      "author":"Herman Melville",
      "title":"Moby Dick",
      "address":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. ",
      "isbn":"0-553-21311-3",
      "price":8.99
    }
  },
  {
    "type":"e-book",
    "shipping":true,
    "available":true,
    "information":{
      "author":"J. R. R. Tolkien",
      "title":"The Lord of the Rings",
      "address":"The Main Library is located on West Circle Drive, south of Grand River Avenue in East Lansing. ",
      "isbn":"0-395-19395-8",
      "price":22.99
    }
  }
]

test with jest ( npm install --save-dev jest )

npm test


with ❤ by @kikerios