# json-xml-parse

> A library for fast JSON to XML transformations.

Latest version **1.3.0** (published 2024-10-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-xml-parse
pnpm add json-xml-parse
yarn add json-xml-parse
bun add json-xml-parse
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high maintenance score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2024-10-22 |
| First published | 2020-08-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 27.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | divij |
| Keywords | jsonToXml, json-xml-parse, json-to-xml |

## Links

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

## 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.3.0 (latest) — 2024-10-22
- 1.2.8 — 2023-09-15
- 1.2.7 — 2023-08-07
- 1.2.6 — 2023-08-02
- 1.2.5 — 2023-08-02
- 1.2.4 — 2023-06-26
- 1.2.3 — 2023-06-25
- 1.2.2 — 2023-01-28
- 1.2.1 — 2021-03-07
- 1.2.0 — 2021-03-07
- 1.1.0 — 2020-08-24
- 1.0.9 — 2020-08-18
- 1.0.8 — 2020-08-18
- 1.0.7 — 2020-08-18
- 1.0.6 — 2020-08-13
- … 6 more at https://npm.io/package/json-xml-parse/versions

## README

# Transform JSON to XML

A library for fast JSON to XML transformations.

## Main Features

* Fast Conversion
* Lightweight
* Dependency-Free
* CLI and Browser Support

## Options

| Name | Type | Default
|---|---|---|
| attrKey | str | "@" |
| contentKey | str | "#" |
| beautify | bool | true |
| selfClosing | bool | true |
| typeHandler | func | (v) => [true, v] |
| declaration | obj | `{ "version": "1.0" }` |
| entityMap | obj | `{ "<": "&lt;", ">": "&gt;", "&": "&amp;", "'": "&apos;", "\"": "&quot;"}` |

## Usage

```js
const parser = require('json-xml-parse');

const data = {
  SolarSystem: {
    Galaxy: "\"MilkyWay\"",
    Star: () => "Sun",
    Planet: [
      {
        "position": "1",
        "name": "<Mercury>",
        "distance": "58",
      },
      {
        "position": "2",
        "name": "Venus",
        "distance": "108"
      }
    ],
    Planet1: {
      "@": {
        position: "3",
        distance: "149"
      },
      name: "Earth"
    },
    Planet2: {
    "@": {
      position: "4",
      distance: 227,
    },
    "#": {
        name: {
          "#" : 'mars',
          "@": {
            "has-water": 'true'
          },
        },
        moon: [
          undefined,
          'Europa',
          'Deimos'
        ]
      }
    }
  }
}

const xml = parser.jsXml.toXmlString(data);
```

```xml
<?xml version="1.0"?>
<SolarSystem>
 <Galaxy>&quot;MilkyWay&quot;</Galaxy>
 <Star>Sun</Star>
 <Planet>
  <position>1</position>
  <name>&lt;Mercury&gt;</name>
  <distance>58</distance>
 </Planet>
 <Planet>
  <position>2</position>
  <name>Venus</name>
  <distance>108</distance>
 </Planet>
 <Planet1 position="3" distance="149">
  <name>Earth</name>
 </Planet1>
 <Planet2 position="4" distance="227">
  <name has-water="true">mars</name>
  <moon/>
  <moon>Europa</moon>
  <moon>Deimos</moon>
 </Planet2>
</SolarSystem>
```

## Type Handling

```js
const options = {
  typeHandler: (v) => [true, v]; 
  // return [<bool for creating tag>, <value>]
  // runs for every nested property
}
```

## Using Options

```js
const Options = {
  beautify: true,
  selfClosing: true,
  typeHandler: (v) => {
    if([undefined, null, ''].includes(v)) {
      return [false, v]
    }
    return [true, v]
  }
}

const data = {
  SolarSystem: {
    Galaxy: undefined,
    Star: () => null,
    Planet: 'Earth'
  }
}
const xml = parser.jsXml.toXmlString(data, Options);
```

```xml
<?xml version="1.0"?>
<SolarSystem>
 <Planet>Earth</Planet>
</SolarSystem>
```

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