# structured-json

> Framework for complex configuration structures

Latest version **2.2.0** (published 2018-01-16) · MIT license · 0 weekly downloads

## Install

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

## 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 | 2.2.0 |
| Published | 2018-01-16 |
| First published | 2018-01-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Winton Welsh |
| Maintainers | winton |
| Keywords | json, configuration |

## Links

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

## Dependencies (1)

- [dependency-graph](https://npm.io/package/dependency-graph.md) ^0.6.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

- 2.2.0 (latest) — 2018-01-16
- 2.1.0 — 2018-01-16
- 2.0.0 — 2018-01-16
- 1.3.0 — 2018-01-05
- 1.2.0 — 2018-01-05
- 1.1.1 — 2018-01-05
- 1.1.0 — 2018-01-05
- 1.0.1 — 2018-01-05
- 1.0.0 — 2018-01-05

## README

# Structured JSON

Operators that make complex JSON structures easy to read and write.

Action                                | Operator     | Key/Value
------------------------------------- | ------------ | ---------
[Assign Value](#assign)               | `<=`         | Value
[Assign Defaults](#defaults)          | `<<`, `>>`   | Key
[Merge](#merge)                       | `<<`, `>>`   | Value
[Mixin](#mixin)                       | `$`          | Key
[Conditional Defaults](#conditionals) | `<<?`, `>>?` | Key

Use the [update](#update) function for immutable updates.

## Install

```bash
npm install structured-json
```

## Import

```js
import { build, update } from "structured-json"
```

## Assign

```js
let { stores, products } = build({
  "stores": {
    "grocery": {
      "products": "<= products"
    }
  },
  "products": {
    "milk": {
      "store": "<= stores.grocery"
    }
  }
})

stores.grocery.products // { milk }
products.milk.store     // { products }
```

Assignment supports circular references, but it is up to you to be careful about infinite enumeration.

## Merge

```js
let { products } = build({
  "organicProducts": {
    "eggs": {},
    "milk": {}
  },
  "veganProducts": {
    "kale": {},
    "tofu": {}
  },
  "products": "<= organicProducts << veganProducts"
})

products // { eggs: {},
         //   milk: {},
         //   kale: {},
         //   tofu: {} }
```

## Defaults

When used in a key, the merge operator defines a default object for its siblings (`>>`) or its parent (`<<`):

```js
let { organicProducts, veganProducts } = build({
  "organicProducts": {
    ">>": { "organic": true },
    "eggs": {},
    "milk": {}
  },
  "veganProducts": {
    ">>": { "vegan": true },
    "kale": {},
    "tofu": {}
  }
})

organicProducts // { eggs: { organic },
                //   milk: { organic } }
veganProducts   // { kale: { vegan },
                //   tofu: { vegan } }
```

Define defaults for sibling child objects with successive merge operators (`">> >>":`).

## Mixin

A mixin is a variable meant only for referencing, and does not show up in enumeration.

```js
let { products } = build({
  "products": {
    "$green": {
      "color": "green"
    },
    "$white": {
      "color": "white"
    },
    "milk": { "<<": "$white" },
    "kale": { "<<": "$green" },
    "tofu": { "<<": "$white" }
  }
})

products // { milk: { color: "white" },
         //   kale: { color: "green" },
         //   tofu: { color: "white" } }
```

## Conditionals

```js
let { products } = build({
  "winter": true,
  "products": {
    ">>? winter": {
      "local": false
    },
    ">>": {
      "local": true
    },
    "kale": {}
  }
})

products // { kale: { local: false } }
```

## Update

```js
let config = build(...)

config = update(config,
  "products.lettuce <<", { "<<": "$green" }  
)
```

The `update` function uses [`immutability-helper`](https://github.com/kolodny/immutability-helper) to create entirely new objects for each update.

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