# json-expand

> Expand self referencing json documents

Latest version **1.4.0** (published 2015-10-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install json-expand
pnpm add json-expand
yarn add json-expand
bun add json-expand
```

Provides the command `json-expand`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2015-10-14 |
| First published | 2015-07-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Josh Vanderwillik |
| Maintainers | joshwillik |
| Keywords | json, expand |

## Links

- npm: https://www.npmjs.com/package/json-expand
- Repository: https://github.com/JoshWillik/json-expand
- Homepage: https://github.com/JoshWillik/json-expand#readme
- Issues: https://github.com/JoshWillik/json-expand/issues
- npm.io page: https://npm.io/package/json-expand

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.4.0 (latest) — 2015-10-14
- 1.3.0 — 2015-09-12
- 1.2.1 — 2015-07-16
- 1.2.0 — 2015-07-16
- 1.1.0 — 2015-07-16
- 1.0.0 — 2015-07-15

## README

# json-expand

Expands your self referencing json

## Installation
Standalone Script
```shell
$ git clone https://github.com/joshwillik/json-expand.git
$ chmod +x json-expand/json-expand.js
$ mv json-expand/json-expand.js ~/my-scripts/json-expand
# Assuming ~/my-scripts is in $PATH
```
NPM module
```shell
$ npm install -g json-expand
```

## CLI Usage
```js
// config.json
{
  "apiDomain": "api.{{baseDomain}}",
  "baseDomain": "{{userAccount}}.{{hostingProvider}}",
  "userAccount": "joshwillik",
  "hostingProvider": "superhost.foobar",
  "authDomain": "{{google.accounts.base}}{{google.accounts.url}}",
  "google": {
    "accounts": {
      "url": "/login",
      "base": "accounts.google.com"
    }
  }
}
```
```shell
$ json-expand < config.json
{
  "apiDomain": "api.joshwillik.superhost.foobar",
  "baseDomain": "joshwillik.superhost.foobar",
  "userAccount": "joshwillik",
  "hostingProvider": "superhost.foobar",
  "authDomain": "accounts.google.com/login",
  "google": {
    "accounts": {
      "url": "/login",
      "base": "accounts.google.com"
    }
  }
}
```

## Node.js usage
```js
var expand = require( 'json-expand' )
var obj = {
    foo: "bar",
    message: "foo = {{foo}}"
}
expand( obj )
> { foo: 'bar', message: 'foo = bar' }
```

## Rules

Any **expression** wrapped in `{{ }}` will be expanded to the correct value or an empty string if none exists.
The expression to expand may come before the thing it will expand to.

```json
{
    "name": "John, son of {{parent}}",
    "parent": "Bill"
}
// becomes
{
    "name": "John, son of Bill",
    "parent": "Bill"
}
```

An expression may reference sub-properties of the document it resides in. All expressions are evaluated starting at the top scope.

```json
{
    "description": "John drives with {{vehicle.wheels}} wheels",
    "vehicle": {
        "wheels":4
    }
}
// becomes
{
    "description": "John drives with 4 wheels",
    "vehicle": {
        "wheels":4
    }
}
```

Using `||` operator will make json-expand attempt to expand each option, in order, until one works. Once one value is valid, the rest of the options are ignored.

```json
{
    "description": "I enjoy {{ favoriteFood || foodIKindaLike }}",
    "foodIKindaLike": "spaghetti"
}
// becomes
{
    "description": "I enjoy spaghetti",
    "foodIKindaLike": "spaghetti"
}
// but
{
    "description": "I enjoy {{ favoriteFood || foodIKindaLike }}",
    "foodIKindaLike": "spaghetti",
    "favoriteFood": "meatballs"
}
// becomes
{
    "description": "I enjoy meatballs",
    "foodIKindaLike": "spaghetti",
    "favoriteFood": "meatballs"
}
```

Environment variables may be substituted by adding a `$` before an expression.
Using `||` works just fine with these.

```json
{
    "remoteHost": "{{ $REMOTE_HOST || defaultHost }}",
    "defaultHost": "production.project.com"
}
// if REMOTE_HOST has been set to 'dev.project.com
{
    "remoteHost": "dev.project.com",
    "defaultHost": "production.project.com"
}
// otherwise
{
    "remoteHost": "production.project.com",
    "defaultHost": "production.project.com"
}
```

Expressions can be concatinated together if desired. The `+` has higher precedence than `||`, so
`{{ foo || bar + ' test'}}` will result in either (foo) or (bar + ' test'), never (foo + ' test').

Using parenthesis to override the order of operations will not work in this version.

```json
{
    "baseDomain": "my-project.com",
    "apiDomain": "{{ doesNotExist || 'api.' + baseDomain }}"
}
// becomes
{
    "baseDomain": "my-project.com",
    "apiDomain": "api.my-project.com"
}
```

---
_Source: https://npm.io/package/json-expand · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
