ConvertFrom-Json and arrays

Welcome Forums General PowerShell Q&A ConvertFrom-Json and arrays

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
12 months ago.

  • Author
    Posts
  • #89068

    Participant
    Points: 0
    Rank: Member

    Hello,

    I'm currently stuck on importing a Json file. I could boil it down to this example:

    '{"FiltList":{"1":"a","2":["b","c"]}}' | ConvertFrom-Json
    
    FiltList                 
    ——–                 
    @{1=a; 2=System.Object[]}
    

    How can I convert the array ["b","c"] to an usefull object instead of "System.Object[]"

    Cheers
    Quirks

  • #89084

    Participant
    Points: 91
    Rank: Member
    $a = '{"FiltList":{"1":"a","2":["b","c"]}}' | ConvertFrom-Json
    $a.FiltList
    $a.FiltList.1
    $a.FiltList.2
    
    1 2     
    - -     
    a {b, c}
    a
    b
    c
    
    • #89092

      Participant
      Points: 0
      Rank: Member

      Some times I'm slow on the uptake. Thanks for the clarification.

  • #89216
    js

    Participant
    Points: 327
    Helping Hand
    Rank: Contributor
    PS /Users/me> ($a.filtlist.2)[0]                                            
    b
    PS /Users/me> ($a.filtlist.2)[1]                                            
    c
    
    • #89899

      Participant
      Points: 0
      Rank: Member

      Just to conclude this topic.
      I was struggling because in my code I've set up an hash table with a hash table inside with an array inside. I've used this structure to do some work.

      After several iterations I've saved this structure to a json file. When importing this json file to continue the work I was running in quite some trouble accessing the values, because what I'm getting back was not my initial structure but a psobject.

      I've solved this problem by initializing the whole structure with psobjects. So my code can access the structure in the first and each following run.

The topic ‘ConvertFrom-Json and arrays’ is closed to new replies.