Announcement

Collapse
No announcement yet.

XML to JSON

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

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

    Comment


    • #3
      You would need to create a custom formula function in Espo.

      Comment


      • #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...
        X