@hulitolku

Как в Powershell сконвертировать JSON в XML?

Имеется файл JSON:
{"id":151,"name":"Ethereum","tag":"ETH","algorithm":"Ethash","block_time":"13.2171","block_reward":3.38164691934411,"block_reward24":2.47394642108815,"block_reward3":2.43106978730364,"block_reward7":2.47354304799483,"last_block":11198117,"difficulty":3.28474428835378e+15,"difficulty24":3.31921075037543e+15,"difficulty3":3.33020803175734e+15,"difficulty7":3.34908949025559e+15,"nethash":248522314906732,"exchange_rate":0.027506,"exchange_rate24":0.0281083902097902,"exchange_rate3":0.028024746353455,"exchange_rate7":0.0282619981498387,"exchange_rate_vol":14606.69535981,"exchange_rate_curr":"BTC","market_cap":"$46,746,537,523","pool_fee":"0.000000","estimated_rewards":"0.005699","btc_revenue":"0.00015676","revenue":"$2.35","cost":"$1.01","profit":"$1.34","status":"Active","lagging":false,"testing":false,"listed":true,"timestamp":1604591418}


Конвертирую его таким образом:
$JsonObject = Get-Content -Path "E:\151.json" | ConvertFrom-Json | ConvertTo-Xml -As Stream -Depth 3 -notypeinformation | Out-File -FilePath "E:\151.xml"


Получаю файл XML такого вида:
spoiler
<?xml version="1.0" encoding="utf-8"?>
<Objects>
<Object>
  <Property Name="id">151</Property>
  <Property Name="name">Ethereum</Property>
  <Property Name="tag">ETH</Property>
  <Property Name="algorithm">Ethash</Property>
  <Property Name="block_time">13.1976</Property>
  <Property Name="block_reward">3,36803885427179</Property>
  <Property Name="block_reward24">2,4885061027176</Property>
  <Property Name="block_reward3">2,43649062821275</Property>
  <Property Name="block_reward7">2,47379219646246</Property>
  <Property Name="last_block">11198314</Property>
  <Property Name="difficulty">3,27979191300323E+15</Property>
  <Property Name="difficulty24">3,318617524144E+15</Property>
  <Property Name="difficulty3">3,33042936760631E+15</Property>
  <Property Name="difficulty7">3,34833730193706E+15</Property>
  <Property Name="nethash">248514268730923</Property>
  <Property Name="exchange_rate">0,027253</Property>
  <Property Name="exchange_rate24">0,0280801203966006</Property>
  <Property Name="exchange_rate3">0,028011932341651</Property>
  <Property Name="exchange_rate7">0,0282536657558909</Property>
  <Property Name="exchange_rate_vol">13953,23715211</Property>
  <Property Name="exchange_rate_curr">BTC</Property>
  <Property Name="market_cap">$47,103,976,168</Property>
  <Property Name="pool_fee">0.000000</Property>
  <Property Name="estimated_rewards">0.005734</Property>
  <Property Name="btc_revenue">0.00015626</Property>
  <Property Name="revenue">$2.38</Property>
  <Property Name="cost">$1.01</Property>
  <Property Name="profit">$1.38</Property>
  <Property Name="status">Active</Property>
  <Property Name="lagging">False</Property>
  <Property Name="testing">False</Property>
  <Property Name="listed">True</Property>
  <Property Name="timestamp">1604594004</Property>
</Object>
</Objects>



Но меня это не устраивает, так как нужно получить XML, такого вида:
spoiler
<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <id>151</id>
  <name>Ethereum</name>
  <tag>ETH</tag>
  <algorithm>Ethash</algorithm>
  <block_time>13.2081</block_time>
  <block_reward>3.2819827821987</block_reward>
  <block_reward24>2.49671503172623</block_reward24>
  <block_reward3>2.43922693788229</block_reward3>
  <block_reward7>2.47496490060655</block_reward7>
  <last_block>11198396</last_block>
  <difficulty>3270117645942950</difficulty>
  <difficulty24>3318196120742750</difficulty24>
  <difficulty3>3330288899805890</difficulty3>
  <difficulty7>3348277101451160</difficulty7>
  <nethash>247584258594570</nethash>
  <exchange_rate>0.027232</exchange_rate>
  <exchange_rate24>0.0280687276536313</exchange_rate24>
  <exchange_rate3>0.0280081347606612</exchange_rate3>
  <exchange_rate7>0.028252038221181</exchange_rate7>
  <exchange_rate_vol>13787.27998122</exchange_rate_vol>
  <exchange_rate_curr>BTC</exchange_rate_curr>
  <market_cap>$46,824,068,374</market_cap>
  <pool_fee>0.000000</pool_fee>
  <estimated_rewards>0.005753</estimated_rewards>
  <btc_revenue>0.00015668</btc_revenue>
  <revenue>$2.38</revenue>
  <cost>$1.01</cost>
  <profit>$1.37</profit>
  <status>Active</status>
  <lagging>false</lagging>
  <testing>false</testing>
  <listed>true</listed>
  <timestamp>1604595158</timestamp>
</root>


Как получить вышеописанный код?
  • Вопрос задан
  • 215 просмотров
Пригласить эксперта
Ответы на вопрос 1
john36allTa
@john36allTa
alien glow of a dirty mind
Криво очень но работает
$JsonObject = Get-Content -Path "E:\151.json" | ConvertFrom-Json | ConvertTo-Xml -notypeinformation | foreach { $_.Objects.Object.OuterXml -replace '\<Property.+?Name="(.+?)".*?\>(.+?)\</Property\>','<$1>$2</$1>' -replace "<(.)?Object>",'<$1root>'} | Out-File -FilePath "E:\151.xml"
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы