Skip to content

Add JSON to XML Translator Interface for Node.js#22

Open
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1763749489-json-xml-translator
Open

Add JSON to XML Translator Interface for Node.js#22
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1763749489-json-xml-translator

Conversation

@devin-ai-integration
Copy link

Description

This PR adds a standalone Node.js tool for translating JSON data to XML format. The tool provides both a programmatic API and a command-line interface, making it easy to convert JSON objects to well-formed XML documents.

Link to Devin run: https://app.devin.ai/sessions/9f29ef634e954b0aa566c8d93ee619c7
Requested by: ronald.kim1@merck.com

Changes

  • Core converter module (index.js): Implements JSON to XML conversion with proper XML character escaping, element name sanitization, and support for nested structures, arrays, and all JSON data types
  • CLI interface (cli.js): Command-line tool with options for customizing root element, indentation, array item names, and output destination
  • Comprehensive test suite (test.js): 24 tests covering XML escaping, name validation, object/array conversion, edge cases, and custom options
  • Documentation (README.md): Detailed usage guide with API reference and examples
  • Example file (example-input.json): Sample JSON for testing the tool

Type of Change

  • New feature
  • Bugfix
  • Refactor
  • Documentation
  • Other

Key Features

  • Converts JSON objects, arrays, and primitive values to XML
  • Proper XML character escaping (&, <, >, ", ')
  • Automatic sanitization of invalid XML element names
  • Customizable root element and array item names
  • Configurable indentation
  • Optional XML declaration
  • No external dependencies (uses only Node.js built-ins)

Testing

All 24 tests pass successfully:

cd tools/json-xml-translator
npm test

Test coverage includes:

  • XML character escaping and special characters
  • XML name validation and sanitization
  • Simple and nested object conversion
  • Array handling (including arrays of objects)
  • Null, boolean, and primitive values
  • Custom options (root element, indentation, array item names)
  • Edge cases (empty objects/arrays, invalid JSON)

Human Review Checklist

Please verify:

  1. Placement: Is tools/json-xml-translator/ the appropriate location for this utility?
  2. XML escaping: Review the escapeXml() function to ensure all special XML characters are properly handled
  3. Element name sanitization: Check the sanitizeKey() function for edge cases with invalid XML element names
  4. CLI functionality: Test the CLI with various inputs to ensure argument parsing and error handling work correctly
  5. Integration: Should this tool be integrated with the existing build/test infrastructure, or is standalone appropriate?

Usage Examples

CLI:

# Convert from file
node cli.js input.json

# Convert from stdin
echo '{"name":"John","age":30}' | node cli.js

# Custom options
node cli.js input.json -o output.xml -r data -i 4

Programmatic API:

import { jsonToXml } from './index.js';

const json = { name: 'John', age: 30 };
const xml = jsonToXml(json, {
  rootElement: 'person',
  indentSize: 4
});

Additional Comments

This is a standalone Node.js tool with no dependencies on the existing Python-based teal-agents infrastructure. It requires Node.js >= 18.0.0 to run.

- Implement core JSON to XML conversion module with proper XML escaping
- Add CLI interface with customizable options (root element, indentation, array item names)
- Include comprehensive test suite with 24 tests covering various data types and edge cases
- Add detailed documentation with usage examples and API reference
- Support for nested objects, arrays, primitives, null values, and special characters
- Automatic sanitization of invalid XML element names

Co-Authored-By: ronald.kim1@merck.com <ronald.kim1@merck.com>
@devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants