locked
Push json string into arraylist convert json element into string instead of remain in its original type RRS feed

  • Question

  • $t=@()
    $r= new-object -type PSObject
    class
    {
    [string]$a
    [string[]]$b
    
    }
    
    $c =get-adgroupmember -recrisive $team| select SamAccountName  
    
    $c.b = $c.SamAccountName
    $c.a="dummy"
    $t += $c
    
    $r | add-member -name 'c' -type noteproperty -value $t
    $r| convertto-json | out-file -filepath .\xyz.json -NOClobber -force
    
    this script give 
    {
    d: [
    "a" : "\"sda"\", "\sdd"\"
    ]
    }
    the json is not properly printed also shall I declare $t in class, the slashes are there



    Wednesday, March 27, 2019 5:06 PM

All replies

  • The code makes no sense. 

    To create Json use a hash:

    $json = @{
          Prop1 = $value
          Prop2 = $value2
          ...
    } | ConvertTo-Json
    


    \_(ツ)_/

    Wednesday, March 27, 2019 5:53 PM
  • I have to push my $userAsJson into $users arraylist that is successful, but Email element of array is converted into string by default finally that  I want avoid

    $users = [System.Collections.ArrayList]::new(); $userAsJson = ' { "isCertified" : true, "Email" : "onemail", "twomail", "threemail" }'; $user = $userAsJson | ConvertFrom-Json; $null = $users.Add($user); $null = $users.Add($user);

    $result = @{ Users = $users}; $result | ConvertTo-Json;

    once I have got final json like below 

    { "Users": [ { "isCertified": true, "Email" : "onemail,twomail,threemail" },

                      {
                          "isCertified":  true,
                      "Email" : "onemail,twomail,threemail"
                      }

    ] }

    I want like

    { "Users": [ { "isCertified": true, "Email" : [ "onemail", "twomail", "threemail" ] },

    {

    "isCertified": true,

    "Email" : [ "onemail", "twomail", "threemail" ] }

    ] }

    ref : https://stackoverflow.com/questions/42861725/how-to-add-multiple-json-objects-to-one-json-object-in-powershell

    Also in above code what $null is doing I don't want to use $null, my dynamic json come as string and finally  I am trim the , and it looking good (only pushing into Users is problem also after push the Email array list  convert into string that I don't want to )
    • Edited by himanshu_kulkarni Saturday, March 30, 2019 5:22 PM about $null asking query
    Saturday, March 30, 2019 5:17 PM
  • Surround your list of Email addresses with "[" and "]":

    $userAsJson = '
    {
        "isCertified" : true,
        "Email" : ["onemail", "twomail", "threemail"]
    }';

    If you want to suppress the output of the ".Add" method you can use either of these two constructs:

    [void]$users.Add($user)
    $users.Add($user) | Out-Null


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Saturday, March 30, 2019 6:16 PM
  • $t=@()
    $r= new-object -type PSObject
    class
    {
    [string]$a
    [string[]]$b
    
    }
    
    $c =get-adgroupmember -recrisive $team| select SamAccountName  
    
    $c.b = $c.SamAccountName
    $c.a="dummy"
    $t += $c
    
    $r | add-member -name 'c' -type noteproperty -value $t
    $r| convertto-json | out-file -filepath .\xyz.json -NOClobber -force
    
    this script give 
    {
    d: [
    "a" : "\"sda"\", "\sdd"\"
    ]
    }
    the json is not properly printed also shall I declare $t in class, the slashes are there



    Changing you original question is not going to fix this.   The problem is that you are not clear on what you are trying to attain.  Telling us how you want to do something without saying what it is you are trying to do won't work.

    First you need to learn basic PowerShell then take the time to learn what Json is. First we would tart by building a hash an adding the values to it.  If that array of hashes is a named elemnt the it is added to a new hash with that name.  After all hashes are built we would convert the result to Json.  You cannot mix the two methods.


    \_(ツ)_/


    • Edited by jrv Saturday, March 30, 2019 7:02 PM
    • Proposed as answer by Nevets24 Thursday, May 23, 2019 12:40 PM
    Saturday, March 30, 2019 7:02 PM
  • I want to achieve this

    {
        "Users":  [
                      {
                          "isCertified":  true,
    		      "Email" : [
    				"onemail",
    				"twomail",
    				"threemail"
    				]
                      }
                 ]
    } 
    
    

    but powershell is giving me this

    { "Users": [ { "isCertified": true, "Email" : "onemail,twomail,threemail" }

    ] }

    My original json string format as below

    $json = @{

    "isCertified" : "true",

    "Email": "onemail",

    "twomail",

    "threemail"

    },{

    "isCertified" : "false",

    "Email": "fourmail",

    "fivemail",

    "sixmail"

    }

      I want to first push it into Users (object array) then convert-to json but finally Email field converted into string array this is issue (it should remain in arraylist only)

    If I will do as per Rich I am getting  "/Email"/ : "onemail"/"twomail/""/threemail/" -- something like this in my email field

    In case there is arraylist for one nested element of json then it finally convertto-json convert string array

    for ex : https://stackoverflow.com/questions/42861725/how-to-add-multiple-json-objects-to-one-json-object-in-powershell on this link in email field you can take multiple email ids into arraylist, you will see finally it is converted into string array like above

    Monday, April 1, 2019 4:20 PM
  • Use a -depth of 3.  The default unfortunately is 2.  You'll get the result you want.

    $r | convertto-json -depth 3

    • Edited by JS2010 Monday, April 1, 2019 5:04 PM
    • Proposed as answer by jrv Monday, April 1, 2019 5:32 PM
    • Unproposed as answer by himanshu_kulkarni Tuesday, April 2, 2019 3:50 PM
    Monday, April 1, 2019 5:02 PM
  • You can't start with Json.  Start with a hash containing only "Users".

    $users = @{Users = @()}

    Create a hash template:

    $template = @{
        isCertified=$false
        Email = $null
    }

    To add a new item clone the template and set the values

    $item = $template.Clone()
    $item.IsCetirfied = [$true/$false]
    $item.Email = $addresses -join ','

    Add it to the base hash
    $users.Users += $item

    Finally convert to Json:

    $users | ConvertTo-Json

    This will create the Json you requested.

    PS D:\scripts> $users | ConvertTo-Json
    {
        "Users":  [
                      {
                          "Email":  null,
                          "isCertified":  true
                      },
                      {
                          "Email":  null,
                          "isCertified":  false
                      }
                  ]
    }
    PS D:\scripts>

    I did not create dummy addresses but any array of addresses will be inserted correctly.

    Note that element order in a hash or in Json is unimportant.



    \_(ツ)_/



    • Edited by jrv Monday, April 1, 2019 5:45 PM
    Monday, April 1, 2019 5:43 PM
  • First thing first, the way you create variable isn't correct in PS, do not use $c.a, not only is it not correct, but you might have some funky reaction when coding. 

    Monday, April 1, 2019 6:06 PM
  • It is pushing into Users till the step is looking good but again Email is converted in to string array like

    "Email":  "onemail, twomail,threemail" etc and requirement is

    "Email":  ["onemail", "twomail","threemail"]

    Tuesday, April 2, 2019 3:54 PM
  • It is pushing into Users till the step is looking good but again Email is converted in to string array like

    "Email":  "onemail, twomail,threemail" etc and requirement is

    "Email":  ["onemail", "twomail","threemail"]

    If you keep changing what you are asking for no one will be able to help you.

    If you use my code and remove the "-join" the addresses will be added as an array.  This is noted in my example.

    Since you do not know PowerShell or Json I suggest talking some time to learn these things before trying to as questions.


    \_(ツ)_/

    Tuesday, April 2, 2019 3:57 PM
  • It is pushing into Users till the step is looking good but again Email is converted in to string array like

    "Email":  "onemail, twomail,threemail" etc and requirement is

    "Email":  ["onemail", "twomail","threemail"]

    Change the convertto-json -depth parameter to 3 or higher and it will be fixed.
    Tuesday, April 2, 2019 4:07 PM
  • Here is the complete example:

    $addresses = 'addr1@domain.com', 'addr2@domain.com', 'addr3@domain.com'
    $users = @{ 
        Users = @() 
    }
    
    #Create a hash template:
    $template = @{
        isCertified = $false
        Email       = $null
    }
    
    for($i = 0; $i -lt 4;$i++){
        
        #To add a new item clone the template and set the values
        $item = $template.Clone()
        $item.isCertified = $true
        $item.Email = $addresses
    
        #Add it to the base hash
        $users.Users += $item
    }
    
    #Finally convert to Json:
    $users | ConvertTo-Json -Depth 3
    


    \_(ツ)_/

    Tuesday, April 2, 2019 4:20 PM
  • You can use the .Split(','), but to be honest I think our fella JRV gave you all the tools to succeed, you still got to try out some stuff out too. I have the feeling that you are just doing a copy/past that's it. I am not trying to be harsh here, but this is a forum to help you out with code. If you are looking for something already scripted, the TechNet Gallery is the place you are looking for. 
    Thursday, April 4, 2019 1:37 PM
  • ConvertTo-Json -Depth 3

    • Edited by JS2010 Thursday, April 4, 2019 7:19 PM
    Thursday, April 4, 2019 5:28 PM