0

I am trying to put the information below as a hash table in Powershell. I think I need to add a hashtable inside a hashtable but I have no idea how to do that. can somebody help?

"credentials":{
  "add":{},
  "edit":{
    "58":{
      "username":"test",
      "auth_method":"Password",
      "password":"test"
    }
  },
  "delete":[]
}

2 Answers 2

1

Does it absolutely have to be a hashtable? It looks like you've got some JSON there (missing a leading {).

In PowerShell v3 and higher, you can do this (note the fixed JSON string):

$json = '{"credentials":{"add":{},"edit":{"58":{"username":"test","auth_method":"Password","password":"test"}},"delete":[]}}' 
$object = $json | ConvertFrom-Json

Now $object will be a PSObject with properties (as opposed to keys); very easy to work with.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Thanks for reply. you are right, I am using json, i tried your method using convertfrom-json, however $object did not show any result. Here is the complete json request.
complete request $jason = '{"uuid":"0625147c-30fe-d79f-e54f-ce7ccd7523e9b63d84cb81c23c2f","settings":{"name":"vivekkk cre"}, "credentials":{"add":{},"edit":{"58":{"username":"test1","auth_method":"Password","password":"test1"}}}}' $show= convertfrom-json -InputObject $jason
0

You could take help from syntax given below to create hash tables inside hash table in Powershell.

   $cred = @{

       1 = @{

          'x' = 'strin1';

          'y' = 'strin2'};

       2 = @{

          'y' = 'Strin3';

          'z' = 'Strin4' } 
}

The above code is self-explanatory( table $cred contains two tables inside it). Implementing this in your actual code is left for you to figure out.

1 Comment

Still not working as expected, Here is what i did till now: $data1= @{"uuid"="0625147c-30fe-d79f-e54f-ce7ccd7523e9b63d84cb81c23c2f"} $settings=@{} $settings.add("name","Windows Malware Scan5") $settings.add("description","Windows Malware Scan5") $data1.add("settings",$settings) $credentials=@{} $credentials.add = ("add", "") $credentials.add = ("edit", $edit) $edit=@{} $edit.add= ("58",$uapd) $uapd=@{} $uapd.add("username","test1") $uapd.add("auth_method","Password") $uapd.add("password","test1") $data1.add("credentials",$credentials)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.