Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell xml to json blank file

Tags:

I'm trying to convert an xml file to json with powershell. Until then it is very simple, however the source file seems to me bad coded and when I apply convertto-json, the structure json is empty:

XML source :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PnCG3_configuration>
  <domains>
    <domain Name="xxxxx">
      <players>
        <player targetId="00-1c-e6-02-0a-00" targetIdType="mac" label="test" middlewareFamily="gekkota-3" />
        <player />
      </players>
    </domain>
    <domain Name="xxxxxxx2">
      <players>
        <player targetId="00-1c-e6-02-0a-00" targetIdType="mac" label="OfficeChateaugiron-TV01" middlewareFamily="gekkota-3" />
        <player targetId="00-1c-e6-02-20-00" targetIdType="mac" label="test-01" middlewareFamily="gekkota-3" />
        <player targetId="00-1c-e6-02-0b-00" targetIdType="mac" label="test-test-TV01" middlewareFamily="gekkota-3" />
        <player targetId="00-1c-e6-02-20-00" targetIdType="mac" label="test-TV01-test" middlewareFamily="gekkota-3" />
        <player targetId="00-1c-e6-02-20-00" targetIdType="mac" label="test" middlewareFamily="gekkota-3" />
        <player targetId="00-1c-e6-02-09-00" targetIdType="mac" label="test-test-TV01" middlewareFamily="gekkota-3" />
        <player targetId="00-1c-e6-02-04-00" targetIdType="mac" label="test-test-TV01" middlewareFamily="gekkota-3" />
        <player />
      </players>
    </domain>

And Json Out :

[
    [

    ],
    [
        [
            [
                [
                    [

                    ],
                    [

                    ]
                ]
            ],
            [
                [
                    [

                    ],
                    [

                    ],
                    [

                    ],
                    [

                    ],
                    [

                    ],
                    [

                    ],

My code for convert :

$xmlObject = [XML](Get-Content -Path $o)
$xmlObject | ConvertTo-JSON -depth 100 | Out-File "$o.json"
like image 513
Steven Poulain Avatar asked Aug 09 '18 09:08

Steven Poulain


People also ask

What is ConvertFrom JSON?

The ConvertFrom-Json cmdlet converts a JavaScript Object Notation (JSON) formatted string to a custom PSCustomObject object that has a property for each field in the JSON string. JSON is commonly used by web sites to provide a textual representation of objects.

How do I convert XML to PowerShell?

Casting XML Strings to Objects Another way to use PowerShell to parse XML is to convert that XML to objects. The easiest way to do this is with the [xml] type accelerator. By prefixing the variable names with [xml] , PowerShell converts the original plain text XML into objects you can then work with.


1 Answers

Sadly it is not that easy.

Have a look at this GitHub Repo which I have used before to do exactly what you're asking for.

Your code would change to:

Add-Type -Path .\Newtonsoft.Json.dll
$xmlObject = [XML](Get-Content -Path $o)
[Newtonsoft.Json.JsonConvert]::SerializeXmlNode($xmlObject ) | Out-File "$o.json"
like image 52
TobyU Avatar answered Oct 04 '22 04:10

TobyU