Home > Blockchain >  How to convert nested json into hashtable in powershell?
How to convert nested json into hashtable in powershell?

Time:01-21

I'm trying to convert json into hashtable in 5.1 version of powershell. But the output is coming as an object again for FieldMapping Key. Can we get the key value pairs for FieldMapping key ?

We have ConvertFrom-Json -AsHashtable in 7.1 version . Ideally trying to get the same o/p in 5.1 as well. Can anyone please help me out regarding the same ?

My json:

$json = '[
 
                              {
 
                                  "EntityType": "IP",
 
                                  "FieldMapping":  [
 
                                                       {
 
                                                           "ColumnName":  "FileHashCustomEntity"
                                                            "Identifier":  "Address"
                                                           
 
                                                       }
 
                                                   ]
 
                              }
 
                          ]'
 

Mycode:

$entities = $json | ConvertFrom-Json 
$ht2 = @{}
$hash = $entities.psobject.properties | Foreach { $ht2[$_.Name] = $_.Value }
echo $ht2

MyOutput:

Key   : EntityType
Value : IP
Name  : EntityType


Key   : FieldMapping
Value : {@{ColumnName=FileHashCustomEntity; Identifier=Address}}
Name  : FieldMapping

Expected Output:

 Key   : EntityType
 Value : IP
 Name  : EntityType


 Key   : FieldMapping
 Value : {FileHashCustomEntity}
 Name  : FieldMapping

CodePudding user response:

Right now you're ending up with a top-level hashtable containing custom objects as entries - what you need to do is convert every object in the resulting object hierarchy recursively.

Here's a simple function that does that:

function Convert-PSCToHashTable
{
  param(
    $InputObject, 
    [int]$Depth = 5
  )

  if($Depth -gt 0){
    if($InputObject -is [System.Collections.IList]){
      return @($InputObject |ForEach-Object {Convert-PSCToHashTable $_ -Depth ($Depth - 1)})
    }

    if($InputObject.psobject.BaseObject -is [System.Management.Automation.PSCustomObject]){
      $ht = @{}
      foreach($prop in $InputObject.psobject.Properties){
        $ht[$prop.Name] = Convert-PSCToHashTable $prop.Value -Depth ($Depth - 1)
      }
      return $ht
    }
  }

  return $InputObject
}

Now you can do:

$object = ConvertFrom-Json $json
$hashtable = Convert-PSCToHashTable $object

$hashtable['FieldMapping']['ColumnName'] # "FileHashCustomEntity"

CodePudding user response:

This works. Included debug statements so you can see how the data is organized

$json = @" 
[
 
                              {
 
                                  "EntityType": "IP",
 
                                  "FieldMapping":  [
 
                                                       {
 
                                                           "ColumnName":  "FileHashCustomEntity",
                                                            "Identifier":  "Address"
                                                           
 
                                                       }
 
                                                   ]
 
                              }
 
                          ]
"@
$entities = $json | ConvertFrom-Json 
$entities
$ht2 = [System.Collections.ArrayList]::new()
$newRow = New-Object -TypeName psobject
$newRow | Add-Member -NotePropertyName EntityType -NotePropertyValue $entities.EntityType
foreach($property in $entities.FieldMapping)
{
$property | Format-Table
   $newRow | Add-Member -NotePropertyName ColumnName -NotePropertyValue $property.ColumnName  
   $newRow | Add-Member -NotePropertyName Identifier -NotePropertyValue $property.Identifier
 
}
$ht2.Add($newRow)  | Out-Null

$ht2 | Format-Table
  • Related