包详细信息

jsonweaver

kledenai44MIT1.1.3

A simple utility to transform JSON data into CSV, XML, YAML, JSONLines and Markdown table formats.

csv, xml, json, data

自述文件

npm version

jsonweaver

jsonweaver is a powerful and easy-to-use library for transforming JSON data into popular formats such as CSV, XML, Markdown tables, YAML, and JSONLines (NDJSON).

Features

  • 🚀 Convert to CSV: Easily transform JSON arrays into CSV files, with optional header mapping and nested object flattening.
  • 📄 Generate Markdown tables: Convert JSON arrays into neatly formatted Markdown tables.
  • 📂 Convert to XML: Transform JSON objects into well-structured XML.
  • 📜 Convert to YAML: Seamlessly transform JSON into YAML format for configuration files and more.
  • 📦 Convert to JSONLines (NDJSON): Convert JSON arrays to JSONLines format for large-scale data processing and streaming.
  • 🛠️ Batch processing: Process large datasets in customizable batches.
  • 🔍 Validate JSON with JSON Schema: Ensure your JSON conforms to specified schemas.
  • 🔧 Compatible with JavaScript and TypeScript: Ideal for modern projects.

Installation

Install using npm:

npm install jsonweaver

Or using Yarn:

yarn add jsonweaver

Usage

Importing

Javascript:

const { jsonweaver } = require("jsonweaver");

TypeScript:

import { jsonweaver } from "jsonweaver";

Examples

JSON to CSV (Basic usage)

const json = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
];

const csv = jsonweaver.toCSV(json);
console.log(csv);

Using custom headers (renaming keys):

const headerMapping = {
  name: "Full Name",
  age: "Years",
};

const csvWithHeaders = jsonweaver.toCSV(
  json,
  jsonweaver.customCSVFieldGenerator(headerMapping)
);
console.log(csvWithHeaders);

Handling nested objects (automatic flattening):

const nestedJson = [
  { name: "Alice", details: { age: 25, city: "Wonderland" } },
  { name: "Bob", details: { age: 30, city: "Gotham" } },
];

const csvFlattened = jsonweaver.toCSV(nestedJson);
console.log(csvFlattened);

JSON to XML

const json = { name: "Alice", age: 25, city: "Wonderland" };
const xml = jsonweaver.toXML(json);
console.log(xml);

JSON to Markdown Table

const json = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
];
const markdownTable = jsonweaver.toMarkdownTable(json);
console.log(markdownTable);

JSON to YAML

const json = { name: "Alice", age: 25, city: "Wonderland" };
const yaml = jsonweaver.toYaml(json);
console.log(yaml);

JSON to JSONLines (NDJSON)

const json = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
];
const jsonLines = jsonweaver.toJsonLines(json);
console.log(jsonLines);

JSON to JSONLines Stream:

const stream = jsonweaver.toJsonLinesStream(json);
stream.on("data", (chunk) => {
  console.log(chunk.toString());
});
stream.on("end", () => {
  console.log("Stream ended.");
});

Validate JSON Schema

const schema = {
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "number" },
  },
  required: ["name", "age"],
};

const data = { name: "Alice", age: 25 };
jsonweaver.validateJsonSchema(data, schema);

Batch Processing

const largeArray = Array.from({ length: 1000 }, (_, i) => ({ id: i }));

await jsonweaver.batchProcess(largeArray, 100, async (batch, batchIndex) => {
  console.log(`Processing batch ${batchIndex}`, batch);
});

API

Function Description
toCSV(json: object[], fieldGenerator?) Converts an array of JSON objects into a CSV string.
toXML(json: object, options?) Converts a JSON object into an XML string.
toMarkdownTable(json: object[]) Converts an array of JSON objects into a Markdown table.
toYaml(json: object) Converts a JSON object into a YAML string.
toJsonLines(json: object[], options?) Converts an array of JSON objects into JSONLines format.
toJsonLinesStream(json: object[]) Creates a stream of JSONLines from an array of JSON objects.
validateJsonSchema(json: object, schema: object) Validates a JSON object against a schema.
batchProcess(data: object[], batchSize: number, processor: (batch, index) => Promise<any>) Processes data in batches asynchronously.

Requirements

  • Node.js version 14.0.0 or higher.

Contribution

Feel free to open issues or submit pull requests to improve jsonweaver. All contributions are welcome! 🌟

  1. Fork the repository.
  2. Create a branch for your feature: git checkout -b my-feature.
  3. Make your changes and commit: git commit -m 'My awesome feature'.
  4. Push to the repository: git push origin my-feature.
  5. Open a pull request on GitHub.

License

This project is licensed under the terms of the MIT License.

更新日志

Changelog

All notable changes to this project will be documented here.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[Unreleased]

  • Optional support for custom flattening behavior in toCSV.
  • Options for customizing YAML output (indentation, quoting styles, etc).
  • Options for customizing Markdown table generation (alignment, style).
  • Support for custom root element name in XML generation.

[1.1.3] - 2025-04-27

Added

  • JSON Schema Validation: New validateJsonSchema function to validate JSON objects against provided schemas using AJV.
  • Batch Processing: New batchProcess function to divide and process large JSON arrays in batches asynchronously.
  • Pretty-Print for XML: XML output now supports customizable pretty formatting through the prettyPrint option.
  • Flatten Helpers: New modular helpers flattenObject and flattenJson to standardize object flattening across converters.
  • Custom CSV Header Mapping: customCSVFieldGenerator added for precise header customization in toCSV.
  • Automatic Flattening in toCSV: Nested JSON objects are automatically flattened in CSV output.
  • Error Handling Improvements: Better error messages and stricter input validation across all main converters.
  • Improved Test Coverage: Full unit test coverage for converters and internal helpers.

Changed

  • Internal refactor: Splitted converters and helpers into modular architecture (@helpers, @converters, etc).
  • Aliases introduced in tsconfig (@converters, @utils, @types) to improve import management and code clarity.
  • Updated internal types for stronger validation (JsonArray, JsonObject, etc).

Fixed

  • Correct flattening of deeply nested objects in CSV generation.
  • Proper handling of empty objects across all converters (returning safe outputs).
  • Small bug fixes and improvements for JSONLines stream generation.

[1.1.2] - 2025-04-19

Added

  • Updated package.json to support both ESM and CJS using exports field and type: module.
  • Updated Jest config to support ESM with --experimental-vm-modules.
  • Adjusted tsconfig.json for compatibility with new build setup.
  • Migrated build system to tsup for faster and modern bundling.
  • Improved GitHub Actions workflow for release publishing.
  • Refactored core imports to use import type syntax.
  • New tsup.config.ts file.

[1.1.1] - 2025-01-10

Added

  • Automation using GitHub Actions to automatically publish to npm when a new release is created.
  • Validates package version to prevent publishing duplicate versions.
  • Executes automated tests (npm test) to ensure package stability.
  • Adds dependency caching to improve CI/CD performance.
  • Configures npm authentication automatically via .npmrc.

[1.1.0] - 2025-01-09

Added

  • JSON to YAML Conversion: Added support for converting JSON to YAML for configurations like Kubernetes and Ansible.
  • JSON to JSONLines (NDJSON): Transform JSON arrays into JSONLines, ideal for incremental processing and data pipelines.
  • JSONLines Stream: Generate readable streams of JSONLines to handle large volumes of data efficiently.

Fixed

  • General improvements in input validation for existing functions.
  • Memory optimizations for large conversions.

[1.0.2] - 2025-01-06

Added

  • Custom header labels for CSV: Allows specifying friendly column names instead of using raw JSON keys.
  • Nested object flattening in CSV: Transforms nested fields (e.g. details.age) into separate columns.
  • Type-based CSV output: Numbers are unquoted, strings are quoted, and null/undefined fields are left empty.
  • Large dataset test coverage: Ensures performance and correctness for bigger JSON arrays.

Fixed

  • Incorrectly quoted numeric fields in CSV outputs.
  • Inconsistent handling of empty or null fields when generating CSV.

[1.0.1] - 2025-01-04

Added

  • Improved XML conversion with options for maximum depth and array handling (wrap or index).
  • Enhanced Markdown table generation with dynamic header extraction.
  • Better error handling for empty JSON arrays in toMarkdownTable and invalid inputs in toXML.
  • New file structure: Organized modules into dedicated files for better maintainability and scalability.

Fixed

  • Corrected edge cases in CSV output when handling missing keys.

[1.0.0] - 2025-01-03

Added

  • 🚀 Convert JSON to CSV, XML, and Markdown tables.
  • 📄 Easy-to-use API: toCSV, toXML, toMarkdownTable.

Fixed

  • Minor bugs in XML formatting.

Notes

  • Minimum Node.js version: 14.0.0.
  • Compatible with JavaScript and TypeScript.

[0.1.0] - 2024-12-25

Added

  • Initial prototype for JSON to CSV conversion.