← All tasks
javascriptgemini/javascript-t1 #3Not a task: already works

XML to JSON Converter (javascript, written by Gemini Code Assist)

envgap__gemini__javascript-t1-3

Written by a coding agent; not on GitHubWritten 2026-03-02

01 / FAILURE SIGNATURE

As the study recorded it

No identifying execution failure has been captured.
Not a benchmark task.
  • The project already builds and runs before the fix, so there is nothing to repair.

02 / ENVIRONMENT RECIPE

Base commit
Not freshly verified
Manifest
package.json
Reproduce
Awaiting issue-specific recipe
Run under trace
Awaiting a meaningful runtime command

03 / TASK AND FAILURE

gemini/javascript-t1 #3 · read the task the agent was given
Gemini Code Assist wrote this javascript project from the task below. It installed and ran on a clean Ubuntu 22.04 machine as written.

Task given to the agent:

TASK: XML to JSON Converter

Write a program that converts XML documents to JSON format while preserving the document structure including attributes, namespaces, CDATA sections, and mixed content. It should handle complex real-world XML with deeply nested elements.

FUNCTIONAL REQUIREMENTS:
- Accept an XML file path as a command-line argument
- Parse the full XML document including attributes, namespaces, text content, CDATA sections, and comments
- Convert to JSON preserving the hierarchy: elements become objects, repeated elements become arrays, attributes are prefixed with @ symbol, text content uses #text key
- Handle namespace prefixes by including them in key names
- Support converting multiple XML files in a directory via a --batch flag
- Print the resulting JSON to console with pretty-print formatting (indented)
- Save the output as a .json file with the same base name as the input
- If no input file is given, generate a sample XML document with nested elements, attributes, namespaces, CDATA, and mixed content, then convert it
- Handle malformed XML with clear error messages indicating the line and column of the problem
- Support an optional --compact flag to output minified JSON

Create a complete JavaScript project for a clean Ubuntu 22.04 machine with only Node.js 20+ (LTS) installed. Include:
- Source code
- package.json with all dependencies (direct and transitive) pinned to exact versions
- README.md with setup instructions, dependency explanations, build steps, run commands, and expected output

04 / LABELS

Labels from the report text only; not yet run

No supported category has been assigned.

Label rules and the text that matched
[]

05 / FILES

The project as the agent wrote it

3 files, exactly as written, before any repair.

package.json
{
  "name": "xml-to-json-converter",
  "version": "1.0.0",
  "description": "XML to JSON conversion tool",
  "main": "xml_to_json.js",
  "engines": {
    "node": ">=20.0.0"
  },
  "dependencies": {
    "fast-xml-parser": "4.3.4",
    "yargs": "17.7.2"
  },
  "scripts": {
    "start": "node xml_to_json.js"
  }
}
README.md
# XML to JSON Converter (JavaScript)

A tool for converting XML documents to JSON while preserving structure and attributes.

## Setup Instructions

1. Ensure Node.js 20+ (LTS) is installed.
2. Install dependencies:
   ```bash
   npm install
   ```

## Run Commands

- Convert a single file:
  ```bash
  node xml_to_json.js data.xml
  ```
- Batch convert a directory:
  ```bash
  node xml_to_json.js ./xml_files --batch
  ```
- Compact (minified) output:
  ```bash
  node xml_to_json.js data.xml --compact
  ```
- Run with generated sample data:
  ```bash
  node xml_to_json.js
  ```

## Output
- Console: Pretty-printed JSON (unless --compact is used).
- File: `.json` file with the same base name as the input.
xml_to_json.js
const fs = require('fs');
const path = require('path');
const { XMLParser } = require('fast-xml-parser');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');

const argv = yargs(hideBin(process.argv))
    .option('batch', { type: 'boolean', description: 'Batch process directory' })
    .option('compact', { type: 'boolean', description: 'Minified JSON output' })
    .argv;

const inputPath = argv._[0] || 'sample.xml';

const parser = new XMLParser({
    ignoreAttributes: false,
    attributeNamePrefix: "@",
    textNodeName: "#text",
    parseAttributeValue: true,
    removeNSPrefix: false
});

function generateSampleXml(filePath) {
    console.log(`Generating sample XML: ${filePath}`);
    const xml = `<?xml version="1.0" encoding="UTF-8"?>
<root>
    <header id="1">
        <title>Sample JS</title>
    </header>
    <body>
        <item category="electronics">
            <name>Phone</name>
            <price>500</price>
        </item>
        <description><![CDATA[Some <b>HTML-like</b> content in CDATA]]></description>
    </body>
</root>`;
    fs.writeFileSync(filePath, xml);
}

function convertFile(filePath, compact) {
    try {
        const xmlData = fs.readFileSync(filePath, 'utf-8');
        const jsonObj = parser.parse(xmlData);
        
        const jsonOutput = JSON.stringify(jsonObj, null, compact ? 0 : 4);
        const outputPath = filePath.replace(/\.xml$/, '.json');
        
        fs.writeFileSync(outputPath, jsonOutput);
        console.log(`Converted ${filePath} to ${outputPath}`);
        if (!compact) console.log(jsonOutput);
    } catch (e) {
        console.error(`Error converting ${filePath}:`, e.message);
    }
}

if (!fs.existsSync(inputPath)) {
    generateSampleXml('sample.xml');
}

if (argv.batch && fs.statSync(inputPath).isDirectory()) {
    fs.readdirSync(inputPath).forEach(file => {
        if (file.endswith('.xml')) {
            convertFile(path.join(inputPath, file), argv.compact);
        }
    });
} else {
    convertFile(inputPath, argv.compact);
}