-1

Before I begin, I have read many posts/topics here/on the internet, but as far as I understand, I think none of the solutions I saw can deal with this JSON


I have a JSON file which I am looking to convert into an XML file - the catch is, when converting to XML normally, JSON like this -

{
    "data": {
        "key4":{
            "sample8": [
                {
                    "sample9":"val",
                    "sample10":"val"
                },
                {
                    "sample11":"val",
                    "sample12":"val"
                },
                {
                    "sample13":"val",
                    "sample14":"val"
                }
            ]
        }
    }
}

becomes -

<?xml version="1.0"?>
<data>
    <key4>
        <sample8>
            <sample9>val</sample9>
            <sample10>val</sample10>
        </sample8>
        <sample8>
            <sample11>val</sample11>
            <sample12>val</sample12>
        </sample8>
        <sample8>
            <sample13>val</sample13>
            <sample14>val</sample14>
        </sample8>
    </key4>
</data>

But, what I am looking to do is create a container element in the XML for every JSON array, with a specific array item element name (like "item"). Here's an example of the XML result I want -

<?xml version="1.0"?>
<data>
    <key4>
        <sample8>
            <item>
                <sample9>val</sample9>
                <sample10>val</sample10>
            </item>
            <item>
                <sample11>val</sample11>
                <sample12>val</sample12>
            </item>
            <item>
                <sample13>val</sample13>
                <sample14>val</sample14>
            </item>
        </sample8>
    </key4>
</data>

How do I do this? As I said, I've tried many different things but nothing seems to work out for me... Kindly guide :) Thanks!


P.S. If PHP doesn't support this kind of conversion, I'll be okay to use any other language which supports doing something like this.

Sonal
  • 137
  • 2
  • 13

1 Answers1

0
$json = <<<JSON
{
  "data": {
    "key4":{
      "sample8": [
        {
          "sample9":"val",
          "sample10":"val"
        },
        {
          "sample11":"val",
          "sample12":"val"
        },
        {
          "sample13":"val",
          "sample14":"val"
        }
      ]
    }
  }
}
JSON;
function toXml($node, $array)
{
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            toXml($node->addChild(is_numeric($key) ? 'item' : $key), $value);
        } else {
            $node->addChild($key, $value);
        }
    }
}
// convert to array
$jsonArr = json_decode($json, true); 
/* 
$jsonArr = [
    "data" => [
        "key4" => [
            "sample8" => [
                [
                    "sample9" => "val",
                    "sample10" => "val"
                ],
                [
                    "sample11" => "val",
                    "sample12" => "val"
                ],
                [
                    "sample13" => "val",
                    "sample14" => "val"
                ]
            ]
        ]
    ]
]
*/
// initiate SimpleXMLElement with the root node and the xlmns:ajson attribute
$xml = new SimpleXMLElement('<data xmlns:ajson="http://www.altova.com/json"/>');
// use array_shift to skip first element (data)
toXml($xml, array_shift($jsonArr));
// your xml
echo $xml->asXML();

If you want to see it with white-spaces and line breaks, you can pass it to DomDocument.

$xmlDocument = new DOMDocument('1.0');
$xmlDocument->preserveWhiteSpace = false;
$xmlDocument->formatOutput = true;
$xmlDocument->loadXML($xml->asXML());

echo $xmlDocument->saveXML();
IGP
  • 14,160
  • 4
  • 26
  • 43
  • Thanks a lot @IGP for the solution! When I ran your code, all I see from echo is `val val val val val val`, which are my values. - How do I see the proper XML contents of the result? - kindly guide... Thanks! – Sonal Nov 23 '21 at 20:02
  • Weird, I can xml as a string just fine. Try `var_dump($xml->asXML());` or `var_dump($xmlDocument->saveXML());` – IGP Nov 23 '21 at 20:56
  • I have a working example at https://replit.com/@-IGP/json-to-xml – IGP Nov 23 '21 at 21:01