2016-05-04 15 views
1

Windows 10でpowershellを使用しています。jsonファイルをかなり印刷されたインデント付きの形式で保存する必要があります。私は幸運と以下のコードの様々な組み合わせを試してみました。Powershellを使用してpretty-printでファイルにJSONを保存する

$url = example.com/api/someThingy $saveAs = people.json Invoke-RestMethod -Uri $url -Method Get -OutFile $saveAs;

JSONサンプル(私は違いが何かを削除可能性があります。)

{"id":"123456","name":"Lorem", "content":null,"purpose":"<p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>\n<p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>","when":"<ul>\n<li>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</li>\n</ul>","Purpose":"<p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>\n<p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>"}

答えて

1

は、以下試してみてください。

$url = example.com/api/people 
$saveAs = people.json 
$jsonContent = Invoke-RestMethod -Uri $url -Method Get; 

$prettyJsonContent = ($jsonContent | ConvertFrom-Json | ConvertTo-Json) 

$prettyJsonContent *> $saveAs 

上記のコードスニペットPowerShellのConvertFrom-JsonConvertTo-Jsonを使用して、JSONをきれいに印刷します。最後の行は、JSONコンテンツを現在のディレクトリのというファイル名に出力します。

+0

このソリューションで重複するキー名に問題がありました。上記の私のサンプルではもう一度表現されていません。 –

+1

サンプルを修正する必要があります。機密情報を削除することの1つですが、問題を再現できるようにする必要があります。 –

+0

私は謝罪する、私はいくつかの機密データが私の問題だと思う。 –

2

ConvertTo-Jsonを使用して保存できます。 Ex。

$url = "example.com/api/people" 
$saveAs = people.json 

Invoke-RestMethod -Uri $url -Method Get | 
ConvertTo-Json | 
Set-Content $saveAs 

サンプル:

Invoke-RestMethod -Uri "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo" | 
ConvertTo-Json | 
Set-Content .\test.txt 
+0

これは以前私のために働いていましたが、何らかの理由でこれが機能しません。 –

+0

働いていないということはどういう意味ですか? '-outfile'と同じ出力ですか? –

+0

節約しましたが、綺麗に塗装していませんでした。 –

0

Json.netを走るには、トリックを行いました。

$newtonSoftPath = "c:\mycode\packages\Newtonsoft.Json.8.0.3\lib\net40\Newtonsoft.Json.dll"; 
[System.Reflection.Assembly]::LoadFile($newtonSoftPath); 

$url = "example.com/api/people" 
$saveAs = "people.json"; 

$result = Invoke-WebRequest -Uri $url -Method Get; 
#for json array 
#$json = [Newtonsoft.Json.Linq.JArray]::Parse($result); 

$json = [Newtonsoft.Json.Linq.JObject]::Parse($result); 
$json.ToString() | Out-File $saveAs; 
関連する問題