# json-sculpt

> JSON Sculpt is a utility library designed to transform or "sculpt" JSON objects into your desired shape.

Latest version **0.6.0** (published 2023-11-05) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2023-11-05 |
| First published | 2023-02-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 33.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | olmoh |
| Keywords | json, reference, typescript, sculpt |

## Links

- npm: https://www.npmjs.com/package/json-sculpt
- npm.io page: https://npm.io/package/json-sculpt

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21

## 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

- 0.6.0 (latest) — 2023-11-05
- 0.5.0 — 2023-06-16
- 0.4.1 — 2023-06-15
- 0.4.0 — 2023-06-07
- 0.3.2 — 2023-05-26
- 0.3.1 — 2023-05-26
- 0.3.0 — 2023-05-26
- 0.2.0 — 2023-02-23
- 0.1.0 — 2023-02-09

## README

# JSON Sculpt

JSON Sculpt is a utility library designed to transform or "sculpt" JSON objects into your desired shape.

## Table of Contents

1. [Installation](#installation)
2. [Usage](#usage)
3. [API](#api)
   - [sculptJson](#sculptJson)
   - [sculptString](#sculptString)
   - [findObjectInJson](#findObjectInJson)
   - [replaceObjectInJson](#replaceObjectInJson)

## Installation

```bash
npm install json-sculpt
```

## Usage

```javascript
import { sculptJson, JsonObject, JsonValue } from "json-sculpt";

const json: JsonObject = {
    name: "Brennan",
    age: 30,
    address: {
        city: "San Francisco",
    },
    {$sculpt$}: {
        name: "{{this.name}}",
        description: "The average age of {{self.address.city}} is {{state.averageAge}}"
    },
};

const state = {
    averageAge: 40,
};

const result = sculptJson<undefined>(json, state);
console.log(result); // { name: "Brennan", description: "The average age of San Francisco is 40" }
```

## API

### sculptJson

Transforms a JSON object into a desired shape.

**Parameters:**

- `json (JsonObject)`: The JSON object to be transformed.
- `replacements (Replacements)`: An object containing custom values to use during sculpting. The values can be either primitives or functions that return primitives. If a function is used, it will be called with the root JSON object as its only argument.
- `options (SculptOptions)`: An object containing options for transforming the JSON.

**Options:**

- `onlyResolveSculpt (bool)`: Should only commands nested under a `{$sculpt$}` object should be resolved, or all values in the JSON object?
- `throwOnError (bool)`: Should an error should be thrown if sculpting fails?

**Returns:**

The sculpted JSON object. If a Result type is provided as a generic parameter, the function will return the specified type.

### sculptString

Replaces all {{}}-enclosed keys with matching values from a state object.

**Parameters:**

- `template (string)`: The string to sculpt.
- `replacements (Replacements)`: Object containing replacment values to replace {{}}-enclosed keys.

**Returns:**

The sculpted string.

### findObjectInJson

Searches for the first object that matches the provided matcher within a JSON structure.

**Parameters:**

- `json (JsonObject)`: JSON object within which to perform the search.
- `matcher (JsonObject)`: JSON object used to match objects within the provided JSON structure.

**Returns:**

The first matched JSON object, or undefined if no match is found.

### replaceObjectInJson

Replaces matched objects within a JSON structure with a specified replacement object.

**Parameters:**

- `json (JsonValue)`: JSON object within which to perform the replacement.
- `replacements (JsonObject)`: Object that will replace matched objects.
- `matcher (JsonObject)`: JSON object used to match objects within the provided JSON structure.

**Returns:**

JSON object with all matched objects replaced with the replacement object.

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