0

My text file has below text in Json format. Its huge file below is sample. I would like this data loaded in hash table so later in script i can query data for processing.

{
    "cohwpcu01":  {
                      "lx-wpcpo1kji04":  6397,
                      "ut14003":  6761,
                      "HFS Citrix 12-2014":  9952,
                      "ut08647":  7678
                   }

    "cohwpcu02":  {
                      "lx-wpcpo1kji05":  6397,
                      "ut140056":  6761,
                      "HFS Citrix 12-2019":  9952,
                      "ut08650":  7678
                    }
}

I have tried below $hash=Get-Content -Path .\hash_table.txt|ConvertFrom-StringData

ConvertFrom-StringData : Data line '{' is not in 'name=value' format. At line:1 char:42

  • $hash=Get-Content -Path .\hash_table.txt|ConvertFrom-StringData
  •                                      ~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [ConvertFrom-StringData], PSInvalidOperationException
    • FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand

Any idea how do i make hash table out of these data ???

Thank you

raju
  • 71
  • 5
  • 1
    PowerShell Core 7+: `$hash=Get-Content -Path .\hash_table.txt -Raw | ConvertFrom-Json -AsHashTable`, otherwise https://stackoverflow.com/q/3740128/7571258 – zett42 Jan 23 '23 at 18:50
  • When i try using new hash table built by above method, getting message "Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'GetEnumerator'." – raju Jan 23 '23 at 22:27
  • 1
    What you're showing us is not a valid Json, its missing a comma to separate the objects. Is this really how your Json looks or is it a bad copy paste? In Windows PowerShell the easiest way to get a hashtable out of your Json is using the `JavaScriptSerializer` as shown in [this answer](https://stackoverflow.com/a/72745321/15339544) – Santiago Squarzon Jan 24 '23 at 01:26

0 Answers0