# junit2json

> Convert JUnit XML format to JSON

Latest version **4.0.0** (published 2026-07-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install junit2json
pnpm add junit2json
yarn add junit2json
bun add junit2json
```

Provides the command `junit2json`.

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2026-07-29 |
| First published | 2020-02-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 2 |
| Unpacked size | 26.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| Author | kesin1202000@gmail.com |
| Maintainers | kesin |
| Keywords | junit, parser |

## Links

- npm: https://www.npmjs.com/package/junit2json
- Repository: https://github.com/Kesin11/ts-junit2json
- Homepage: https://github.com/Kesin11/ts-junit2json#readme
- Issues: https://github.com/Kesin11/ts-junit2json/issues
- npm.io page: https://npm.io/package/junit2json

## Dependencies (2)

- [xml2js](https://npm.io/package/xml2js.md) 0.6.2
- [@types/xml2js](https://npm.io/package/@types/xml2js.md) 0.4.14

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 4.0.0 (latest) — 2026-07-29
- 3.1.16-0 (beta) — 2025-08-11
- 3.2.1 — 2026-04-16
- 3.2.0 — 2025-09-21
- 3.1.16 — 2025-08-12
- 3.1.15 — 2025-07-05
- 3.1.14 — 2025-03-01
- 3.1.13 — 2025-01-01
- 3.1.12 — 2024-09-07
- 3.1.11 — 2024-06-16
- 3.1.10 — 2024-05-27
- 3.1.10-2 — 2024-05-26
- 3.1.10-1 — 2024-05-26
- 3.1.10-0 — 2024-05-18
- 3.1.9 — 2024-05-17
- … 41 more at https://npm.io/package/junit2json/versions

## README

# ts-junit2json
[![junit2json](https://badgen.net/npm/v/junit2json)](https://www.npmjs.com/package/junit2json)
[![JSR](https://jsr.io/badges/@kesin11/junit2json)](https://jsr.io/@kesin11/junit2json)
![CI](https://github.com/Kesin11/ts-junit2json/workflows/Node%20CI/badge.svg)

ts-juni2json provides a converter that convert JUnit XML format to JSON. Also provides TypeScript types definition.

And also provide [CLI](#CLI) that can convert a JUnit XML to JSON.

# Purpose
ts-junit2json is created for uploading test result data to BigQuery.

Many languages and test frameworks supporting output test result data as JUnit XML format that de fact standard in today. On the other hand, BigQuery does not support to import XML but does support JSON.

You notice that you can upload test data to BigQuery with converting XML to JSON. ts-junit2json provides a simple JUnit XML to JSON converter for that purpose.

# What is difference for other XML to JSON tools?
The purpose of other similar tools is handling common XML format. As a result, output JSON structure is sometimes redundant and not suitable for store in BigQuery.

On the other hand, ts-junit2json only supports JUnit XML schema, but restructures original XML structure into a BigQuery friendly structure. Details are described below.

# Installing
```bash
npm install junit2json
```

# Usage
## Node.js
junit2json is an ESModule only package since v4.0.0.

> [!NOTE]
> Until v3, junit2json also shipped a CommonJS build. It was dropped in v4.0.0.
> CommonJS users on Node.js 20.19.0+ or 22.12.0+ can still `require('junit2json')`
> thanks to the stable `require(esm)` support.

```ts
import { parse } from 'junit2json'

const main = async () => {
  const xmlString = `<?xml version="1.0" encoding="UTF-8"?>
  <testsuites name="gcf_junit_xml_to_bq_dummy" tests="2" failures="1" time="1.506">
    <testsuite name="__tests__/basic.test.ts" errors="0" failures="0" skipped="0" timestamp="2020-01-26T13:45:02" time="1.019" tests="1">
      <testcase classname="convert xml2js output basic" name="convert xml2js output basic" time="0.01">
      </testcase>
    </testsuite>
    <testsuite name="__tests__/snapshot.test.ts" errors="0" failures="1" skipped="0" timestamp="2020-01-26T13:45:02" time="1.105" tests="1">
      <testcase classname="parse snapshot nunit failure xml" name="parse snapshot nunit failure xml" time="0.013">
        <failure>Error: Something wrong.</failure>
      </testcase>
    </testsuite>
  </testsuites>
  `

  const output = await parse(xmlString)
  console.log(JSON.stringify(output, null, 2))
}
main()
```

## Deno
junit2json also published to jsr.io as `@kesin11/junit2json`.
If you using Deno, you can import from jsr as below.

```ts
import { parse } from "jsr:@kesin11/junit2json";
```

# Output sample
```json
{
  "name": "gcf_junit_xml_to_bq_dummy",
  "tests": 2,
  "failures": 1,
  "time": 1.506,
  "testsuite": [
    {
      "name": "__tests__/basic.test.ts",
      "errors": 0,
      "failures": 0,
      "skipped": 0,
      "timestamp": "2020-01-26T13:45:02",
      "time": 1.019,
      "tests": 1,
      "testcase": [
        {
          "classname": "convert xml2js output basic",
          "name": "convert xml2js output basic",
          "time": 0.01
        }
      ]
    },
    {
      "name": "__tests__/snapshot.test.ts",
      "errors": 0,
      "failures": 1,
      "skipped": 0,
      "timestamp": "2020-01-26T13:45:02",
      "time": 1.105,
      "tests": 1,
      "testcase": [
        {
          "classname": "parse snapshot nunit failure xml",
          "name": "parse snapshot nunit failure xml",
          "time": 0.013,
          "failure": [
            {
              "inner": "Error: Something wrong."
            }
          ]
        }
      ]
    }
  ]
}
```

# Filter out some tags
If you want to filter out some tags like `<system-out>` or `<system-err>`, you can use `replacer` function argument in [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).

```ts
const output = await parse(xmlString)
const replacer = (key: any, value: any) => {
  if (key === 'system-out' || key === 'system-err') return undefined
  return value
}
console.log(JSON.stringify(output, replacer, 2))
```

# Notice
ts-junit2json changes the structure of some tags for simpler and more consistent output.

- XML Tag inner text is set to value of 'inner' key.
  - The only exceptions are `<system-out>` and `<system-err>`. These inner text is set to the string array.
  - Example: [snapshot.test.ts.snap](./__tests__/__snapshots__/snapshot.test.ts.snap)

# CLI
```bash
npx junit2json junit.xml

# with full options
npx junit2json -p -f system-out,system-err junit.xml
```

```
junit2json - Convert JUnit XML format to JSON

Positionals:
  path  JUnit XML path (use '-' for stdin)                             [string]

Options:
      --help                        Show help                          [boolean]
      --version                     Show version number                [boolean]
  -p, --pretty                      Output pretty JSON[boolean] [default: false]
  -f, --filter-tags                 Filter XML tag names               [string]

Examples:
  # Output pretty JSON with filter <system-out> and <system-err> tags.
  npx junit2json -p -f system-out,system-err junit.xml

  # Pipe node --test junit output into junit2json
  node --test --test-reporter=junit ... | npx junit2json -p -
```

## CLI with `jq` examples
### Count testcases

```bash
npx junit2json junit.xml | jq .tests
```

### Show testsuite names

```bash
npx junit2json junit.xml | jq .testsuite[].name
```

### Show testcase classnames

```bash
npx junit2json junit.xml | jq .testsuite[].testcase[].classname
```

# References
JUnit XML format

- https://llg.cubic.org/docs/junit/
- https://github.com/junit-team/junit5/blob/main/platform-tests/src/test/resources/jenkins-junit.xsd

# API reference
https://jsr.io/@kesin11/junit2json/doc

# License
MIT

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