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

Why it has "Object" rather than some value? #599

Closed
1 task
Mnemosynel opened this issue Jul 24, 2023 · 3 comments
Closed
1 task

Why it has "Object" rather than some value? #599

Mnemosynel opened this issue Jul 24, 2023 · 3 comments

Comments

@Mnemosynel
Copy link

  • [Y ] Are you running the latest version?
  • [Y ] Have you included sample input, output, error, and expected output?
  • [Y ] Have you checked if you are using correct configuration?
  • [N ] Did you try online tool?

Description

Input

const data = `
<items>
  <item>
    <name>Item 1</name>
    <price>10.99</price>
  </item>
  <item>
    <name>Item 2</name>
    <price>19.99</price>
  </item>
</items>
`;

Code

function parseOriginFragmentDetail(data: string): any {
  const options = {
    ignoreAttributes: true,
    ignoreNameSpace: true,
    parseAttributeValue: false,
    parseNodeValue: true,
    allowBooleanAttributes: true
  };

  const parser = new XMLParser(options);
  const jsonObj = parser.parse(data);

  return jsonObj;
}
let jsonData1 = parseOriginFragmentDetail(data);
console.log(jsonData1);
console.log("***************");

Output

{ items: { item: [ [Object], [Object] ] } }


expected data

 {
    "items": {
        "item": [
            {
                "name": "Item 1",
                "price": 10.99
            },
            {
                "name": "Item 2",
                "price": 19.99
            }
        ]
    }
}

Would you like to work on this issue?

  • [Y ] Yes
  • No

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

@github-actions
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.

@cecia234
Copy link
Contributor

Hi! I think that's how console.log() works when printing an array of objects, when the array itself is contained in a nested object.
For example, if you try to print a similar object with this script:

const aaa = {
    a:{
        b: [ { d: 1 }, { e: 3 } ]
    }
}
console.log(aaa);

You obtain

{ a: { b: [ [Object], [Object] ] } }

If you need the object parsed as string you can use JSON.stringify().
With your script it should look something like this:

const data = `
<items>
  <item>
    <name>Item 1</name>
    <price>10.99</price>
  </item>
  <item>
    <name>Item 2</name>
    <price>19.99</price>
  </item>
</items>
`;

function parseOriginFragmentDetail(data: string): any {
  const options = {
    ignoreAttributes: true,
    ignoreNameSpace: true,
    parseAttributeValue: false,
    parseNodeValue: true,
    allowBooleanAttributes: true
  };

  const parser = new XMLParser(options);
  const jsonObj = parser.parse(data);

  return jsonObj;
}
let jsonData1 = parseOriginFragmentDetail(data);
console.log(JSON.stringify(jsonData1));
console.log("***************");

You should see this in the console

{"items":{"item":[{"name":"Item 1","price":10.99},{"name":"Item 2","price":19.99}]}}
***************

@Mnemosynel
Copy link
Author

It works! Thanks!

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

No branches or pull requests

2 participants