# json-cascade

> pure json cascading template library for browser or node.js

Latest version **1.0.3** (published 2015-01-06) · MIT license · 0 weekly downloads

## Install

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

## 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.0.3 |
| Published | 2015-01-06 |
| First published | 2014-12-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Kevin Sheedy |
| Maintainers | kevinsheedy |
| Keywords | json, cascade, template |

## Links

- npm: https://www.npmjs.com/package/json-cascade
- Repository: https://github.com/KevinSheedy/json-cascade
- Issues: https://github.com/KevinSheedy/json-cascade/issues
- npm.io page: https://npm.io/package/json-cascade

## Dependencies (1)

- [underscore](https://npm.io/package/underscore.md) ^1.7.0

## 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.0.3 (latest) — 2015-01-06
- 1.0.2 — 2015-01-06
- 1.0.1 — 2015-01-05
- 1.0.0 — 2015-01-04
- 0.1.0 — 2014-12-17

## README

# json-cascade

json-cascade is a pure json template(ish) library for browser or node.js. It takes several json objects and combines them to produce a single output. Like CSS, it uses cascading and precedence to determine which fields get output.

```javascript
jsonCascade(template [, diff1] [, diffN])
```

## Usage nodejs
```javascript
jsonCascade = require('json-cascade');
out = jsonCascade(template [, diff1] [, diffN]);
```

## Usage browser
```html
<script src="underscore.js"></script>
<script src="json-cascade.js"></script>

out = jsonCascade(template [, diff1] [, diffN]);
```

## Template
The template object defines the structure of the output json. Only fields that appear in the template will be part of the output.

## Diffs
Each diff (aka model) is then applied to the template with the last diff having highest precedence.


## Simple Example

### Template
```javascript
{
	customerId : null,
	firstname : "",
	lastname : "",
	age : -1
}
```

### Diff One
```json
{
	"firstname" : "John",
	"age" : 32
}
```


### Diff Two
```json
	{
		"lastname" : "Smith",
		"age" : 48
	}
```

### Output
```javascript
{
	customerId : null,
	firstname : "John",
	lastname : "Smith",
	age : 48
}
```

## Arrays
To output an array of objects, the template should contain an array with exactly **one object** (not a primitive). The equivalent array in the diff may then have multiple objects which will be applied in a loop. If the template array does not contain exactly one object, the array will be overridden as if it were a primitive.



## Complex Example

### Template
```javascript
{
	customerId : null,
	firstname : "",
	lastname : "",
	age : -1,
	address : {
		line1 : "",
		line2 : "",
		line3 : "",
		country : ""
	},
	relatedCustomers : [],
	products : [
		{
			productId : -1,
			title : "",
			price : -1
		}
	]
}
```

### Diff One
```javascript
{
	firstname : "John",
	age : 32,
	address : {
		line1 : "Aras An Uachtairain",
	},
	relatedCustomers : [2000005],
	products : [
		{
			productId : 1000005,
			title : "The Mythical Man Month",
			price : 50
		},
		{
			productId : 1000006,
			title : "Effective Java",
			price : 40
		}
	]
}
```

### Diff Two
```javascript
{
	lastname : "Smith",
	age : 48,
	address : {
		line2 : "Phoenix Park",
		country : "Ireland"
	},
	relatedCustomers : [2000008, 2000009],
	products : [
		{
		},
		{
			title : "Clean Code",
			price : 100
		},
		{
			productId : 1000009
		}
	]
}
```

### Output
```javascript
{
	customerId : null,
	firstname : "John",
	lastname : "Smith",
	age : 48,
	address : {
		line1 : "Aras An Uachtairain",
		line2 : "Phoenix Park",
		line3 : "",
		country : "Ireland"
	},
	relatedCustomers : [2000008, 2000009],
	products : [
		{
			productId : -1,
			title : "",
			price : -1
		},
		{
			productId : 1000006,
			title : "Clean Code",
			price : 100
		},
		{
			productId : 1000009,
			title : "",
			price : -1
		}
	]
}
```

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