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

Add xml2json function #446

Closed
mtuchi opened this issue Dec 7, 2023 · 1 comment
Closed

Add xml2json function #446

mtuchi opened this issue Dec 7, 2023 · 1 comment

Comments

@mtuchi
Copy link
Collaborator

mtuchi commented Dec 7, 2023

Short Summary

Add xml2json function for easy conversion of xml string to json

Why ?

We need to traverse the XML response from a SOAP based API into a json object so that we can easily map and transform the data

Why not use parseXML?

Well we have tried using parseXML to traverse the XML response but it's not easy to map the XML response, You have to go through the cheerio documentation and figure out how to traverse the xml response

See example here 👇🏽

fn((state) => {
  return {
    ...state,
    data: {
      body: "<xml><<response>cheeseburger</response></xml>",
    },
  };
});

// Extract and parse the data from the response payload
parseXML(dataValue("body"), ($) => {
  const response = $("response");
  const $inner = $.load(response.text());

  const payload = [];
  $inner("payload ledger line").each((i, elm) => {
    payload.push({
      res: $(this).find("menu").text,
    });
  });
  console.log(payload)
});

The approach above might work but it's difficult to troubleshoot, there is no documentation for it on our adaptors and not easy getting proper mapping that you can confidently trust not to break

Side Note

Currently cheerio [The library we are using behind parseXML] has a method called $.exctract which seems to provide a simple interface for extracting data from HTML document. The down side is although this function is documented, it not release yet [See GitHub issue here]. A library that was born out of that GitHub issue - https://github.com/denkan/cheerio-json-mapper which might be useful for adding $.exctract ability in parseXML

Implementation Note

Add xml2json function with the following signature

See options list here - https://github.com/Leonidas-from-XIV/node-xml2js?search=1#options
interface Xml2JsonOptions {
  [key: string]: any;
}
xml2json(xmlString: string, options: Xml2JsonOptions, callback: (state: any) => any))
@mtuchi
Copy link
Collaborator Author

mtuchi commented Dec 11, 2023

Not needed for now

@mtuchi mtuchi closed this as completed Dec 11, 2023
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

1 participant