XML to JSON

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PavelZ
    Member
    • Jan 2023
    • 30

    XML to JSON

    Hi, I use Send HTTP Request (Workflow). The server response is in XML. How can I convert XML to JSON? Thank you
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    Found this possible solution: https://stackoverflow.com/questions/...rt-xml-to-json

    Comment

    • yuri
      Member
      • Mar 2014
      • 8440

      #3
      You would need to create a custom formula function in Espo.
      If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #4
        You just need a custom formula function that will convert the xml to json, try this below:

        1 - Create a file custom/Espo/Custom/Core/Formula/Functions/JsonGroup/ConvertXmlToJsonType.php with the code:

        Code:
        <?php
        
        namespace Espo\Custom\Core\Formula\Functions\JsonGroup;
        
        use Espo\Core\Formula\Functions\BaseFunction;
        use Espo\Core\Formula\ArgumentList;
        
        class ConvertXmlToJsonType extends BaseFunction
        {
        public function process(ArgumentList $args)
        {
        $args = $this->evaluate($args);
        
        if (count($args) < 1) {
        $this->throwTooFewArguments();
        };
        
        $xml = $args[0];
        
        // Load xml data into xml data object
        $xmldata = simplexml_load_string($xml);
        
        // Encode this xml data into json using json_encode function
        $jsondata = json_encode($xmldata);​
        
        return $jsondata;
        }
        }​​
        2 - Create a file custom/Espo/Custom/Resources/metadata/app/formula.json and add the code:
        Code:
        {
        "functionList": [
        "__APPEND__",
        {
        "name": "xml\\convertToJson",
        "insertText": "xml\\convertToJson(VALUE)"
        }
        ],
        
        "functionClassNameMap": {
        "xml\\convertToJson": "Espo\\Custom\\Core\\Formula\\Functions\\JsonGroup\\ConvertXmlToJsonType"
        }
        }​​
        I have not tested the code but i think it should work. you can add more logic if you wish.

        Good luck
        Rabii
        Web Dev

        Comment

        Working...