I am creating a JSON in Powershell and I want to set a custom tab width when building it (instead of the default 4 white spaces I want to set only 2 white spaces).
I am
You can use Newtonsoft.Json with PowerShell. There is a module for it in PowerShell Gallery that you can install to make it available in a convenient way.
Example:
if (!(Get-Module -ListAvailable -Name "newtonsoft.json")) {
Install-Module -Name "newtonsoft.json" -Scope CurrentUser -Force
}
Import-Module "newtonsoft.json" -Scope Local
$JObject = [Newtonsoft.Json.Linq.JObject]::new(
[Newtonsoft.Json.Linq.JProperty]::new("Phone", "SomePhone"),
[Newtonsoft.Json.Linq.JProperty]::new("Description", "Lorem ipsum dolor.."),
[Newtonsoft.Json.Linq.JProperty]::new("Price", 99.99));
$JObject.ToString()
Produces
{
"Phone": "SomePhone",
"Description": "Lorem ipsum dolor..",
"Price": 99.99
}
It has tons of other features when working with json as well: https://www.newtonsoft.com/json/help/html/Introduction.htm