0

My Api (laravel) use Json code. Now I also want to support xml. The Xml message must be converted to Json so it fits into the existing structure. But I can not match the XML to the desired Json format.

Example XML and Convert to Json:

        $xml = "<?xml version='1.0'?>
    <catalog>
       <book>
            <author>Jan</author>
            <title>Xml to Json Array</title>
            <pages>
                <page>
                    <nr>1</nr>
                    <title>Welcome to my book</title>
                    <pagereviews>
                        <pagereview>
                            <name>maikel</name>
                            <comment>very good</comment>
                        </pagereview>
                        <pagereview>
                            <name>John</name>
                            <comment>i like this page</comment>
                        </pagereview>                            
                    </pagereviews>
                </page>
                <page>
                    <nr>2</nr>
                    <title>more info</title>
                </page>  
                <page>
                    <nr>3</nr>
                    <title>lots of fun</title>
                </page>                              
            </pages>
       </book>
    </catalog>";

    $xmlObject = simplexml_load_string($xml);

    $jsonString = json_encode($xmlObject);

    return $jsonString;

In Json this results in:

{
"book": {
    "author": "Jan",
    "title": "Xml to Json Array",
    "pages": {
        "page": [
            {
                "nr": "1",
                "title": "Welcome to my book",
                "pagereviews": {
                    "pagereview": [
                        {
                            "name": "maikel",
                            "comment": "very good"
                        },
                        {
                            "name": "John",
                            "comment": "i like this page"
                        }
                    ]
                }
            },
            {
                "nr": "2",
                "title": "more info"
            },
            {
                "nr": "3",
                "title": "lots of fun"
            }
        ]
    }
}

}

I need it without tags Page and Pagereview. Like this.

{
   "book": {
      "author": "Jan",
      "title": "Xml to Json Array",
      "pages": [
         {
            "nr": "1",
            "title": "Welcome to my book",
            "pagereviews": [
               {
                  "name": "maikel",
                  "comment": "very good"
               },
               {
                  "name": "John",
                  "comment": "i like this page"
               }
            ]
         },
         {
            "nr": "2",
            "title": "more info"
         },
         {
            "nr": "3",
            "title": "lots of fun"
         }
      ]
   }
}
user3229579
  • 131
  • 2
  • 6
  • 1
    Can you show your code? – Aleks G Dec 17 '18 at 10:09
  • $xmlObject = simplexml_load_string($xml); $jsonString = json_encode($xmlObject); return $jsonString; – user3229579 Dec 17 '18 at 10:11
  • 1
    That simply converts one to another - keeping exact structure. You need to change the structure, so you'll need to write the code to parse your data. Start there - then come back with specific issues you're having – Aleks G Dec 17 '18 at 10:12
  • What do you need - do you want to change the original XML or is that fixed? – Nigel Ren Dec 17 '18 at 10:12
  • I think, you can't change that while converting to json. But you can get array and remove undesired levels – splash58 Dec 17 '18 at 10:13

1 Answers1

-1

I made several packages that integrate with Laravel to simplify XML. You can check them out here:

If your xml needs to differ in structure from json, what I would recommend you do is look into Spatie's Fractal package. It will introduce transformers for your data. Then you can conditionally set transformers before using my packages to have the data coming out exactly as you want.

Mark
  • 1,255
  • 1
  • 13
  • 25
  • im not sure but it looks like the Request XML would satify the OP requirements would'nt it? – blamb Dec 17 '18 at 19:58
  • Generally, links to tools or libraries [should be accompanied by usage notes, a specific explanation of how the linked resource is applicable to the problem, or some sample code](//meta.stackoverflow.com/a/251605/584192), or if possible all of the above. – Samuel Liew Dec 19 '18 at 10:51