{"id":13598973,"url":"https://github.com/EliteLoser/ConvertTo-Json","last_synced_at":"2025-04-10T10:30:46.892Z","repository":{"id":139133203,"uuid":"88099264","full_name":"EliteLoser/ConvertTo-Json","owner":"EliteLoser","description":"Svendsen Tech's ConvertTo-STJson is a pure-PowerShell ConvertTo-Json for PowerShell version 2","archived":false,"fork":false,"pushed_at":"2023-10-20T17:25:15.000Z","size":115,"stargazers_count":21,"open_issues_count":1,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-06T23:40:57.379Z","etag":null,"topics":["convertto-json","convertto-stjson","json","json-api","powershell","powershell-module","powershell-script","psv2","splunk","splunk-http","svendsentech"],"latest_commit_sha":null,"homepage":"https://www.powershelladmin.com/wiki/ConvertTo-Json_for_PowerShell_version_2","language":"PowerShell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EliteLoser.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-12T21:51:49.000Z","updated_at":"2024-02-09T08:10:39.000Z","dependencies_parsed_at":"2024-01-14T04:44:03.140Z","dependency_job_id":"7289bfd3-0fa7-4d1c-a0b7-58871c7c7c47","html_url":"https://github.com/EliteLoser/ConvertTo-Json","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EliteLoser%2FConvertTo-Json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EliteLoser%2FConvertTo-Json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EliteLoser%2FConvertTo-Json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EliteLoser%2FConvertTo-Json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EliteLoser","download_url":"https://codeload.github.com/EliteLoser/ConvertTo-Json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248199136,"owners_count":21063641,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["convertto-json","convertto-stjson","json","json-api","powershell","powershell-module","powershell-script","psv2","splunk","splunk-http","svendsentech"],"created_at":"2024-08-01T17:00:58.640Z","updated_at":"2025-04-10T10:30:46.597Z","avatar_url":"https://github.com/EliteLoser.png","language":"PowerShell","funding_links":[],"categories":["PowerShell"],"sub_categories":[],"readme":"# ConvertTo-Json\n\nConvertTo-STJson is a pure-PowerShell ConvertTo-Json that's compatible with PowerShell version 2.\n\nYou can read about JSON syntax here: http://json.org\n\nIf you have PowerShell version 3 or higher, it's already built into the system.\n\nOnline blog documentation: https://www.powershelladmin.com/wiki/ConvertTo-Json_for_PowerShell_version_2\n\nThere's a \"Send-STSplunkMessage\" script/function in this repo here if you're interested in ad hoc splunking from PowerShell (v2 and up):\nhttps://github.com/EliteLoser/Send-SplunkMessage\n\nComplex example object screenshot:\n\n![alt tag](/ConvertTo-STJson-complex-structure-example2.png)\n\nResulting JSON from ConvertTo-STJson:\n\n![alt tag](/ConvertTo-STJson-complex-structure-json-output-example2.png)\n\nFor a while calculated properties caused bugs for now (sort of) known reasons (some sort of inheritance), but it now works directly. I implemented a conversion of DateTime objects to a different type, ISO8601 string `(2023-10-20T19:22:00)`, like the PowerShell team's ConvertTo-Json does (in unpredictable ways).\n\n```powershell\nGet-ChildItem wat.psd1 | Select FullName, Name, LastWriteTime,\n    @{ Name = 'MeasuredTime'; Expression = { [DateTime]::Now } } |\n    ConvertTo-STJson\n{\n    \"FullName\": \"C:\\\\temp\\\\wat.psd1\",\n    \"LastWriteTime\": \"03/09/2017 19:40:21\",\n    \"MeasuredTime\": \"04/14/2017 18:32:34\",\n    \"Name\": \"wat.psd1\"\n}\n```\n\nHere's a demonstration of how to do the same as above using a ForEach-Object and the -InputObject parameter working on $_ and its properties.\n\n```powershell\nPS C:\\temp\u003e Get-ChildItem wat.psd1 | Select FullName, Name, LastWriteTime |\nForEach-Object { ConvertTo-STJson -InputObject @{\n    FullName = $_.FullName\n    Name = $_.Name\n    LastWriteTime = $_.LastWriteTime\n    MeasuredTime = [DateTime]::Now # trying to add\n} }\n{\n    \"FullName\": \"C:\\\\temp\\\\wat.psd1\",\n    \"Name\": \"wat.psd1\",\n    \"MeasuredTime\": \"04/13/2017 04:41:55\",\n    \"LastWriteTime\": \"03/09/2017 19:40:21\"\n}\n```\n\nWith -Compress:\n\n```powershell\nGet-ChildItem wat.psd1 | Select FullName, Name, LastWriteTime,\n    @{ Name = 'MeasuredTime'; Expression = { [DateTime]::Now } } |\n    ConvertTo-STJson -Compress\n{\"FullName\":\"C:\\\\temp\\\\wat.psd1\",\"LastWriteTime\":\"03/09/2017 19:40:21\",\"MeasuredTime\":\"04/14/2017 18:31:20\",\"Name\":\"wat.psd1\"}\n```\n\nAnother demonstration of the -Compress parameter introduced in v0.8.\n\n```powershell\n. C:\\Dropbox\\PowerShell\\ConvertTo-Json\\ConvertTo-STJson.ps1;\n@{\n    a = @{ a1 = 'val\\t\\nue1'; a2 = 'va\\\"lue2'; a3 = @(1, 't\\wo\\b---\\f', 3) }\n    b = \"test\", \"42.3e-10\"\n    c = [pscustomobject] @{ c1 = 'value1'; c2 = \"false\"; c3 = \"null\" }\n    d = @( @{ foo = 'bar' }, @{ foo2 = 'bar2';\n    foo_inner_array = @( @{ deephash = @(@(1..4) + @('foobar', @{ hrm = 'hrmz' }));\n    deephash2 = [pscustomobject] @{ a = 1 } }  )})\n} | ConvertTo-STJson -Compress\n\n{\"c\":{\"c1\":\"value1\",\"c2\":\"false\",\"c3\":\"null\"},\"d\":[{\"foo\":\"bar\"},{\"foo_inner_array\":[{\"dee\nphash2\":{\"a\":1},\"deephash\":[1,2,3,4,\"foobar\",{\"hrm\":\"hrmz\"}]}],\"foo2\":\"bar2\"}],\"b\":[\"test\"\n,\"42.3e-10\"],\"a\":{\"a1\":\"val\\\\t\\\\nue1\",\"a2\":\"va\\\\\\\"lue2\",\"a3\":[1,\"t\\\\wo\\\\b---\\\\f\",3]}}\n```\n\nAs of v0.8.2, calculated properties also work.\n\n```powershell\n[PSCustomObject] @{ testkey = 'testvalue' } | Select *, @{ n='added'; e={'yep, added'}} | ConvertTo-STJson\n{\n    \"added\": \"yep, added\",\n    \"testkey\": \"testvalue\"\n}\n```\n\nPassing through $true and $false as of v0.9.2, but it turns out $null is buggy, but only when passed in as a _single value_ (would essentially just be passed through). Will look into it. It works as a value anywhere else (array or PSobject/hash value).\n\n```powershell\nPS C:\\temp\u003e ($false | ConvertTo-STJson) -eq $false\nTrue\n\nPS C:\\temp\u003e ($true | ConvertTo-STJson) -eq $true\nTrue\n```\n\nComparing my cmdlet to the PowerShell team's. DateTime objects are another story still. I'm unsure why they chose the \\/Date(01234567...)\\/ approach - and also with \"meta properties\" added (but not always...). As of 2018-06-25, I handle dates with the -DateTimeAsISO8601 parameter (terrible name). See the separate section below.\n\n```powershell\n\u003e $ComplexObject = @{\n    a = @{ a1 = \"value`nwith`r`nnewlines\"; a2 = 'va\\\"lue2'; a3 = @(1, 'tw\"o\"', 3) }\n    b = \"test\\u0123\\foo\", \"42.3e-10\", \"2.34\", 2.34\n    c = [pscustomobject] @{ c1 = 'value1'; c2 = \"false\"; c3 = \"null\" }\n    d = @( @{ foo = 'bar/barb' }, @{ foo2 = 'bar2';\n    foo_inner_array = @( @{ deephash = @(@(1..4) + @('foobar', \n    @{ boobar = @{ nullvalue = $null; nullstring = 'null';\n    trueval = $true; falseval = $false; falsestring = \"false\" }}));\n    deephash2 = [pscustomobject] @{ a = 1.23 } }  )})\n}\n\n# PS team cmdlet output vs. mine (mine works on PSv2).\n\u003e ($ComplexObject | ConvertTo-Json -Compress -Depth 99) -eq `\n  ($ComplexObject | ConvertTo-STJson -Compress)\nTrue\n```\n\n# DateTime handling\n\nSpecify the -DateTimeAsISO8601 parameter (or just -date for short since it's uniquely identifying in the parameter set...) to get dates formatted in the format: `yyyy-MM-ddTHH:mm:ss` (e.g. `2018-06-25T12:29:00` for today at the time of writing).\n\n```powershell\nPS C:\\temp\u003e @{ key = @((get-date), (get-date).AddDays(-1)) } | convertto-stjson -DateTimeAsISO8601\n{\n    \"key\":\n    [\n        \"2018-06-25T12:27:32\",\n        \"2018-06-24T12:27:32\"\n    ]\n}\n\nPS C:\\temp\u003e @{ key = @((get-date), (get-date).AddDays(-1)) } | convertto-stjson -DateTimeAsISO8601 -Compress\n{\"key\":[\"2018-06-25T12:27:45\",\"2018-06-24T12:27:45\"]}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEliteLoser%2FConvertTo-Json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEliteLoser%2FConvertTo-Json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEliteLoser%2FConvertTo-Json/lists"}