Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The CLI will now output a quoted string with escape characters instead of raw json like the previous version. #711

Closed
6 of 7 tasks
angeld7 opened this issue Feb 18, 2025 · 6 comments · Fixed by #712
Closed
6 of 7 tasks

Comments

@angeld7
Copy link
Contributor

angeld7 commented Feb 18, 2025

  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?
  • Have you checked the docs for helpful APIs and examples?

Description

The CLI now outputs a string instead of valid JSON, like the previous versions did.

Examples:
version 4.5.1

echo '<xml><item>test</item></xml>' | fxparser 
{
    "xml": {
        "item": "test"
    }
}

version 4.5.2

echo '<xml><item>test</item></xml>' | fxparser 
"{\n    \"xml\": {\n        \"item\": \"test\"\n    }\n}"

Input

<xml><item>test</item></xml>

Code

echo '<xml><item>test</item></xml>' | fxparser 

Output

"{\n \"xml\": {\n \"item\": \"test\"\n }\n}"

expected data

{ "xml": { "item": "test" } }

Would you like to work on this issue?

  • Yes
  • No

I cannot commit much time but I have opened a PR that fixes it locally for me. I'm not familiar enough with the project to add unit tests that will ensure this doesn't become a future regression.

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

Copy link

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.

@amitguptagwl
Copy link
Member

I tested in my local environment and it is working fine.

{
  "xml": {
    "item": "test"
  }
}

I'm not sure if it is OS specific issue. Can you please recheck with v5 and confirm me back?

@angeld7
Copy link
Contributor Author

angeld7 commented Feb 19, 2025

@amitguptagwl I'm using this purely as a CLI tool for my Jenkins pipeline to read the pom.xml. I tried on both Linux via a Jenkins pipeline and MacOS. Here is the full script to recreate including the install scripts.

#!/bin/bash

# If you already have fxparser installed uninstall it
npm uninstall -g --silent fast-xml-parser

# Install via npm
npm install -g --silent fast-xml-parser

echo '<xml><item>test</item></xml>' | fxparser 2> /dev/null

When I save that into fxparser-issue.sh then run it, this is the output on any machine I have tried:

Output:

./fxparser-issue.sh 
"{\n    \"xml\": {\n        \"item\": \"test\"\n    }\n}"

If I update the install command to use the previous version like this npm install -g [email protected] then the script works as expected.

It looks like in the code JSON.stringify is being called on the object twice in some cases. This method in the original code seems to be the culprit

  const callback = function(xmlData) {
    let output = '';
    if (validate) {
      const parser = new XMLParser(options);
      output = parser.parse(xmlData,validate);
    } else if (validateOnly) {
      output = XMLValidator.validate(xmlData);
      process.exitCode = output === true ? 0 : 1;
    } else {
      const parser = new XMLParser(options);
      output = JSON.stringify(parser.parse(xmlData,validate), null, 4);
    }
    if (outputFileName) {
      writeToFile(outputFileName, output);
    } else {
      console.log(JSON.stringify(output, null, 2));
    }
  };

When validate == false and validateOnly == false and outputFileName == null, JSON.stringify is called twice.

@angeld7
Copy link
Contributor Author

angeld7 commented Feb 19, 2025

I updated my pr (#712) to fully address the issue. The issue only occurs when you call fxparser CLI with no flags.

@amitguptagwl
Copy link
Member

let me check throughly. We can't avoid JSON parse.

@angeld7
Copy link
Contributor Author

angeld7 commented Feb 20, 2025

The update in the #712 will still call parse and stringify, it will just do it one time in all cases as expected.

amitguptagwl pushed a commit that referenced this issue Feb 20, 2025
* #711 Removed string formating on cli output

* Updated to fix regression of issue in #701
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 a pull request may close this issue.

2 participants