Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 198 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PowerShell 2.0 ConvertFrom-Json and ConvertTo-Json implementation

#1
I would like to monkeypatch a PowerShell 2.0 environment where the upgrade to 3.0 is not possible at this time.

I am looking for a PowerShell 2.0 script implementation of the `ConvertFrom-Json` cmdlet and `ConvertTo-Json` cmdlet that are in PowerShell 3.0.

I am most interested in the `ConvertFrom-Json`, but `ConvertTo-Json` would also be nice.
Reply

#2
Code with javascriptSerializer return objects with Dictionary inside. Modern convertfrom-JSON (4.0+) return objects only. This code transform deserialize object to modern output :)

function Iterate-Tree($jsonTree) {
$result = @()
foreach ($node in $jsonTree) {
$nodeObj = New-Object psobject
foreach ($property in $node.Keys) {
if ($node[$property] -is [System.Collections.Generic.Dictionary[String, Object]] -or $node[$property] -is [Object[]]) {
$inner = @()
$inner += Iterate-Tree $node[$property]
$nodeObj | Add-Member -MemberType NoteProperty -Name $property -Value $inner
} else {
$nodeObj | Add-Member -MemberType NoteProperty -Name $property -Value $node[$property]
#$nodeHash.Add($property, $node[$property])
}
}
$result += $nodeObj
}
return $result
}

function ConvertFrom-Json20{
[cmdletbinding()]
Param (
[parameter(ValueFromPipeline=$true)][object] $PS_Object
)

add-type -assembly system.web.extensions
$PS_JavascriptSerializer=new-object system.web.script.serialization.javascriptSerializer
$PS_DeserializeObject = ,$PS_JavascriptSerializer.DeserializeObject($PS_Object)

#Convert Dictionary to Objects
$PS_DeserializeObject = Iterate-Tree $PS_DeserializeObject

return $PS_DeserializeObject
}



Reply

#3
function ConvertTo-Json20([object] $item){
add-type -assembly system.web.extensions
$ps_js=new-object system.web.script.serialization.javascriptSerializer
return $ps_js.Serialize($item)
}

function ConvertFrom-Json20([object] $item){
add-type -assembly system.web.extensions
$ps_js=new-object system.web.script.serialization.javascriptSerializer

#The comma operator is the array construction operator in PowerShell
return ,$ps_js.DeserializeObject($item)
}

If you're getting the error:

Add-Type : Could not load file or assembly 'System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 or one of its dependencies. The system cannot find the file specified. "

... these registry commands can be run ([more details][1]):

reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1 /f
reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1 /f

[1]:

[To see links please register here]

Reply

#4
A little bit improved option:



function Iterate-Tree($jsonTree) {
$result = @()
foreach ($node in $jsonTree) {
$nodeObj= New-Object psobject
foreach ($property in $node.Keys) {
if(-not ($property)){
continue }

if ($node[$property] -is [System.Collections.Generic.Dictionary[String, Object]] -or $node[$property] -is [Object[]]) {
$nodeObj | Add-Member -MemberType NoteProperty -Name $property -Value (Iterate-Tree $node[$property])
} else {
$nodeObj | Add-Member -MemberType NoteProperty -Name $property -Value $node[$property]
#$nodeHash.Add($property, $node[$property])
}
}
$result += $nodeObj
}
return [Object]$result
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through