Eric Rohlfs
Eric Rohlfs

Reputation: 1830

Saving JSON to a file with pretty-print using Powershell

I'm using powershell on Windows 10. I need to save the json file in a pretty-print indented format. I have tried various combinations of the code below with no luck.

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

JSON Sample (There is the possibility that I removed something that makes a difference.)

{"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>"}

Upvotes: 2

Views: 6107

Answers (3)

Eric Rohlfs
Eric Rohlfs

Reputation: 1830

Running through Json.net did the trick.

$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;

Upvotes: 1

Frode F.
Frode F.

Reputation: 54971

You can use ConvertTo-Json and save that. Ex.

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

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

Sample:

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

Upvotes: 4

Aman Sharma
Aman Sharma

Reputation: 1990

Try the below:

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

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

$prettyJsonContent *> $saveAs

The above code snippet uses PowerShell's ConvertFrom-Json and ConvertTo-Json to indent the JSON into pretty-print. Then the last line just prints the JSON content to file names as $saveAs in the current directory.

Upvotes: 2

Related Questions