https://github.com/terran-source/interpolate-json
Interpolate a Javascript Object or string with json - Advanced
https://github.com/terran-source/interpolate-json
interpolation javascript json json-interpolation nodejs string-interpolation
Last synced: 3 months ago
JSON representation
Interpolate a Javascript Object or string with json - Advanced
- Host: GitHub
- URL: https://github.com/terran-source/interpolate-json
- Owner: Terran-Source
- License: mit
- Created: 2020-01-01T09:26:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-09-27T20:57:45.000Z (over 1 year ago)
- Last Synced: 2025-03-08T14:36:12.688Z (3 months ago)
- Topics: interpolation, javascript, json, json-interpolation, nodejs, string-interpolation
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/interpolate-json
- Size: 568 KB
- Stars: 11
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# interpolate-json [](https://www.npmjs.com/package/interpolate-json)
 [](https://www.npmjs.com/package/interpolate-json) [](LICENSE)
Interpolate a Javascript Object (json) or string with (another) **json** - Advanced (or ***Substitution***, as others may like to call it).
Minimalist & lightweight ;) approach to handle interpolation, which packs way more punch than simple string *parameter* replacement.
Supports:
1. `${string}` interpolation
2. `${json}` interpolation
3. `${multi.level}` json notation
4. single `${= JavaScript.expression() =}` evaluation
5. custom `{{parameter_boundary}}` declaration## Install
```bash
# with npm
npm install interpolate-json# or with Yarn
yarn add interpolate-json
```## Usage
#### Declaration
```javascript
// declare the varible at the beginning
const interpolation = require('interpolate-json').interpolation;
// or
const { interpolation } = require('interpolate-json');
```
```typescript
// or the import syntax
import { interpolation } from 'interpolate-json';
```#### string
```javascript
// String
let someString = 'I want to be ${character} in ${business.type} by being a ${business.post}';
let values = {
character: 'a Hero',
business: {
type: 'saving people',
post: 'Doctor',
},
};
someString = interpolation.expand(someString, values);
console.log(someString);
// output: I want to be a Hero in saving people by being a Doctor// or using ENVIRONMENT_VARIABLES
// test-string.js
let someString = "Hi, my name is '${USER_NAME}'. I'm ${USER_AGE}";
console.log(interpolation.expand(someString, process.env));
// execute using: USER_NAME='John' USER_AGE=19 node test-string.js
// output: Hi, my name is 'John'. I'm 19
```#### json
```javascript
// Json
let myJson = {
port: '8080',
server: 'www.example.com',
user: 'abcd',
password: 'P@ss#ord',
url: 'https://${user}:${= encodeURIComponent(${password}) =}@${server}:${port}'
};
console.log(interpolation.expand(myJson)); // Look for values inside itself
// output:
{
"port": "8080",
"server": "www.example.com",
"user": "abcd",
"password": "P@ss#ord",
"url": "https://abcd:P%40ss%[email protected]:8080"
}// Let's sweeten the deal with ENVIRONMENT_VARIABLES
// test-json.js
let myJson = {
port: '${PORT}',
server: 'www.example.com',
user: '${=${USER_NAME}.toLowerCase()=}',
password: '${USER_PASSWORD}',
url: 'https://${user}:${= encodeURIComponent(${password}) =}@${server}:${port}'
};console.log(interpolation.expand(myJson));
// execute using: PORT=8080 USER_NAME='John' USER_PASSWORD='P@ss#ord' node test-json.js
// output:
{
"port": "8080",
"server": "www.example.com",
"user": "john",
"password": "P@ss#ord",
"url": "https://john:P%40ss%[email protected]:8080"
}```
> Notice that `${= =}` notation. It's a cool way to use JavaScript expression (not expression_s_, yet, just a single line).
Get the full documentation in [WIKI](https://github.com/Terran-Source/interpolate-json/blob/master/WIKI.md)