1

I need to convert JSON file into hashtable using PowerShell. I read json file, convert its content to an object, then i tried to create a method which should parse powershell object and convert it into hashtable, which values cannot be hashtables or custom objects. An example of JSON:

{
  "obj": {
    "field1" : "value1",
    "field2": {
      "field3": "value3",
      "field4": "hello, world!"
    }
  },
  "other": "abcde"
}

I should get a hashtable with all keys with type string. So, result should be:

Name               | Value
obj.field1         | value1
obj.field2.field3  | value3
obj.field2.field4  | hello, world!
obj.other          | abcde

tried to use some recursion, it doesn't work. Code i have:

function Get-AxJsonNodesList
{
param
(
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [string]$fileName
)

function ConvertTo-Hashtable
{
    [OutputType("hashtable")]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$JsonContent,
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [string]$JsonContentKey,
        [Parameter(Mandatory = $true)]
        [ValidateNotNull()]
        [AllowEmptyString()]
        [string]$Parent
    )

    function Merge-HashTables 
    { 
        param 
        ( 
            [Parameter(Mandatory = $true)] 
            [ValidateNotNull()] 
            [hashtable]$left, 
            [Parameter(Mandatory = $true)] 
            [ValidateNotNull()] 
            [hashtable]$right 
        ) 
        $merged = @{} 
        foreach ($key in $left.keys) 
        { 
            $merged.add($key, $left[$key]) 
        } 
        foreach ($key in $right.keys) 
        { 
            $merged.add($key, $right[$key]) 
        } 
        $merged 
    } 

    if ($jsonContent -eq $null)
    {
        return $null
    }
    $result = @{}
    $p = $parent + $(if ($parent -eq "") { "" } else { "." })
    if ($JsonContent -is [System.Collections.IEnumerable] -and $JsonContent -isnot [string])
    {
        foreach ($objectKey in $JsonContent.Keys)
        {
            $hashTableToAdd = ConvertTo-Hashtable -JsonContent $JsonContent[$objectKey] -JsonContentKey $objectKey -Parent $p
            $result = Merge-HashTables $result $hashTableToAdd
        }
    }
    elseif ($JsonContent -is [PSObject])
    {
        foreach ($property in $JsonContent.PSObject.Properties)
        {
            $hashTableToAdd = ConvertTo-Hashtable -JsonContent $property.Value -JsonContentKey $property.Value -Parent $p
            $result = Merge-HashTables $result $hashTableToAdd
        }
    }
    else
    {
        $result[$p + $JsonContentKey] = $JsonContent
    }
    $result
}

ConvertTo-Hashtable (Get-Content $fileName | ConvertFrom-Json) "" ""
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Since you're first converting to a custom object (`[pscustomobject]`) via `ConvertFrom-Json` anyway, the linked question should provide an answer, specifically [this one](http://stackoverflow.com/a/34383413/45375). – mklement0 Oct 17 '18 at 17:58
  • As for the nested object part, see [PowerShell convert nested JSON array into separate columns in CSV file](https://stackoverflow.com/questions/45829754/powershell-convert-nested-json-array-into-separate-columns-in-csv-file/46081131#46081131) – iRon Oct 18 '18 at 07:47

0 Answers0