# json-cyclic

> Small utility library to replace circular references in JavaScript with JSONPaths

Latest version **1.0.2** (published 2020-07-26) · MIT license · 0 weekly downloads

## Install

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

## 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.2 |
| Published | 2020-07-26 |
| First published | 2018-07-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 23.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | John Flockton |
| Maintainers | thegreatercurve |
| Keywords | JSON, circular JSON, circular references, cyclic JSON, cyclic references |

## Links

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

## 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.2 (latest) — 2020-07-26
- 1.0.1 — 2020-07-26
- 0.0.3 — 2019-05-12
- 0.0.2 — 2018-07-21
- 0.0.1 — 2018-07-21

## README

## JSON Cyclic 

[![CircleCI](https://circleci.com/gh/thegreatercurve/json-cyclic.svg?style=svg)](https://circleci.com/gh/thegreatercurve/json-cyclic)

A small JavaScript library to replace circular references in object literals with [JSONPath](http://goessner.net/articles/JsonPath/) references, so that the data can be stringified as JSON.

It also supports re-inserting the circular data after the JSON has been parsed.

This will fix the below TypeErrors:

```
Chrome: "TypeError: Converting circular structure to JSON"
Firefox: "TypeError: cyclic object value"
Edge: "TypeError: Circular reference in value argument not supported"
Safari: "TypeError: JSON.stringify cannot serialize cyclic structures."
```

## Features
 - Accepts Arrays, Objects, or both in combination
 - Works with >IE8
 - Tiny (~1kB minified)
 - No dependencies

## Installation

```
npm install json-cyclic
```

## Usage

```js
// ES2015
import { encycle, decycle } from "json-cyclic"

// CommonJS
const JSONCyclic = require("json-cyclic") 
```

## API

There are only two methods exposed, and neither require any configuration: 

### `decycle`
Removes any circular data structures. 

See the below usage examples: 

```js
// Arrays
const arr= [1, "a"]

arr[2] = arr;

JSON.stringify(decycle(arr)); // "{"foo":{"bar":{"$ref":"$.foo"}}}"

// Objects
const obj = { foo: { bar: null } };

obj.foo.bar = obj.foo;

JSON.stringify(decycle(obj)); // "{"foo":{"bar":{"$ref":"$.foo"}}}"
```

### `encycle`

Re-inserts any circular data.

See the below: 

```js
const arr = [1, "a", { $ref: "$" }]

encycle(arr)

console.log(arr[2] === arr) // true

JSON.stringify(encycle(arr)); // TypeError: Converting circular structure to JSON
```

## Tests

```
npm test
```

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