title: Converting XML to JSON with Powershell
body: <p>I am trying to convert an xml file to json for further processing.
  This is actually first time I use powershell so I need some help.</p>
  <p>I get an xml file with this structure:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;https://www.something...&quot;?&gt;
  &lt;tabela&gt;
      &lt;naslov&gt;Robno knjigovodstvo - pregled automatskog stanja zaliha artikala&lt;/naslov&gt;
      &lt;dataset&gt;qu1RobaZalihe&lt;/dataset&gt;
      &lt;datum_kreiranja&gt;12.02.2021&lt;/datum_kreiranja&gt;
      &lt;zaglavlje&gt;&lt;sifra&gt;Šifra&lt;/sifra&gt;&lt;naziv&gt;Naziv&lt;/naziv&gt;&lt;katbroj&gt;Kataloški broj&lt;/katbroj&gt;&lt;cfJedmj&gt;JedMj&lt;/cfJedmj&gt;&lt;stanje&gt;Stanje&lt;/stanje&gt;&lt;cijena&gt;Nabavna cijena&lt;/cijena&gt;&lt;datum&gt;Datum dokumenta&lt;/datum&gt;&lt;/zaglavlje&gt;
      &lt;redovi&gt;
              &lt;red&gt;
              &lt;sifra&gt;1&lt;/sifra&gt;
              &lt;naziv&gt;CORE HIT&lt;/naziv&gt;
              &lt;katbroj&gt;A4B0000006606&lt;/katbroj&gt;
              &lt;cfJedmj&gt;KOM&lt;/cfJedmj&gt;
              &lt;stanje&gt;1&lt;/stanje&gt;
              &lt;cijena&gt;100,00&lt;/cijena&gt;
              &lt;datum&gt;01.01.2021&lt;/datum&gt;
          &lt;/red&gt;
          &lt;red&gt;
              &lt;sifra&gt;2&lt;/sifra&gt;
              &lt;naziv&gt;CORE HIT&lt;/naziv&gt;
              &lt;katbroj&gt;A4B0000008340&lt;/katbroj&gt;
              &lt;cfJedmj&gt;KOM&lt;/cfJedmj&gt;
              &lt;stanje&gt;8&lt;/stanje&gt;
              &lt;cijena&gt;100,00&lt;/cijena&gt;
              &lt;datum&gt;01.01.2021&lt;/datum&gt;
          &lt;/red&gt;
          ...
      &lt;/redovi&gt;
  &lt;/tabela&gt;
  </code></pre>
  <p>I need to ger an array of objets separated with comma like this:</p>
  <pre><code>    [
      {
          &quot;sifra&quot;:  &quot;1&quot;,
          &quot;naziv&quot;:  &quot;CORE HIT 7050&quot;,
          &quot;jed_mj&quot;:  &quot;KOM&quot;,
          &quot;kolicina&quot;:  &quot;1&quot;,
          &quot;skladiste&quot;:  &quot;1&quot;,
          &quot;pn&quot;:  &quot;TNX:A4B0000006606&quot;
      },
      {
          &quot;sifra&quot;:  &quot;2&quot;,
          &quot;naziv&quot;:  &quot;CORE HIT 7020&quot;,
          &quot;jed_mj&quot;:  &quot;KOM&quot;,
          &quot;kolicina&quot;:  &quot;8&quot;,
          &quot;skladiste&quot;:  &quot;1&quot;,
          &quot;pn&quot;:  &quot;TNX:A4B0000008340&quot;
      },
      {
          &quot;sifra&quot;:  &quot;3&quot;,
          &quot;naziv&quot;:  &quot;SYSTEM SW LICENCE AND CD-ROM&quot;,
          &quot;jed_mj&quot;:  &quot;KOM&quot;,
          &quot;kolicina&quot;:  &quot;1&quot;,
          &quot;skladiste&quot;:  &quot;1&quot;,
          &quot;pn&quot;:  &quot;TNX:A4B0000007760&quot;
      },
      ...
  ]
  </code></pre>
  <p>... but instead I get only objects like this:</p>
  <pre><code>    {
      &quot;sifra&quot;:  &quot;1&quot;,
      &quot;naziv&quot;:  &quot;CORE HIT 7050&quot;,
      &quot;pn&quot;:  &quot;TNX:A4B0000006606&quot;,
      &quot;jed_mj&quot;:  &quot;KOM&quot;,
      &quot;kolicina&quot;:  &quot;1&quot;,
      &quot;skladiste&quot;:  &quot;1&quot;
  }
  {
      &quot;sifra&quot;:  &quot;2&quot;,
      &quot;naziv&quot;:  &quot;CORE HIT 7020&quot;,
      &quot;pn&quot;:  &quot;TNX:A4B0000008340&quot;,
      &quot;jed_mj&quot;:  &quot;KOM&quot;,
      &quot;kolicina&quot;:  &quot;8&quot;,
      &quot;skladiste&quot;:  &quot;1&quot;
  }
  {
      &quot;sifra&quot;:  &quot;3&quot;,
      &quot;naziv&quot;:  &quot;SYSTEM SW LICENCE AND CD-ROM&quot;,
      &quot;pn&quot;:  &quot;TNX:A4B0000007760&quot;,
      &quot;jed_mj&quot;:  &quot;KOM&quot;,
      &quot;kolicina&quot;:  &quot;1&quot;,
      &quot;skladiste&quot;:  &quot;1&quot;
  }
  </code></pre>
  <p>So far I managed to figure out some things but not all :)
  Code I use to get this is following:</p>
  <pre><code>[xml]$xml = Get-Content xml-skladiste.XML
  
  foreach ($atr in $xml.tabela.redovi.red)
  {
          $prop = [ordered]@{
          'sifra'    = $atr.sifra
          'naziv'    = $atr.naziv
          'pn'    = $atr.katbroj
          'jed_mj'    = $atr.cfJedmj
          'kolicina'    = $atr.stanje
          'skladiste' = &quot;1&quot;
      }
      New-Object -Type PSCustomObject -Property $prop | ConvertTo-Json | Add-Content -Path &quot;output.json&quot;
  }
  </code></pre>
  <p>What I have to add in order to get desirable structure?</p>