ConvertFrom-Json and arrays

This topic contains 4 replies, has 3 voices, and was last updated by  Quirks 1 month ago.

  • Author
    Posts
  • #89068

    Quirks
    Participant

    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

    Sam Boutros
    Participant
    $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

      Quirks
      Participant

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

  • #89216

    js
    Participant
    PS /Users/me> ($a.filtlist.2)[0]                                            
    b
    PS /Users/me> ($a.filtlist.2)[1]                                            
    c
    
    • #89899

      Quirks
      Participant

      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.

You must be logged in to reply to this topic.