INVALID_JSON_ATTRIBUTE Error in powershell

Welcome Forums General PowerShell Q&A INVALID_JSON_ATTRIBUTE Error in powershell

Viewing 1 reply thread
  • Author
    Posts
    • #238655
      Participant
      Topics: 11
      Replies: 9
      Points: 110
      Rank: Participant

      Hi All

      I am trying to write a Hash table for the below JSON code in power shell script and getting INVALID_JSON_ATTRIBUTE error
      Example JSON CODE
      "actions" : [ {
           "action" : "CONN_POOL_STATS",
           "resources" : [ {
             "cluster" : true
           } ]
         }, {
           "action" : "COLL_STATS",
           "resources" : [ {
             "collection" : "",
             "db" : "staging"
           } ]
         } ]
      Here is the Hash table i tried for the above JSON code and assigned it to a variable newrole and using Convertto-JSON at the end.
      $newrole = @{
      actions = @(
                     @{
                     action = 'CONN_POOL_STATS'
                      resources = @(
                                    @{
                      cluster = "true"
                                     }   
                                    )
                       }
                      @{
                    action = 'COLL_STATS'
                     resources = @(
                                   @{
                      collection = ''
                      db = 'staging'
                                    } 
                                  )
                       }
                 )
      $newrole2 = $newrole | ConvertTo-Json
      
      Here is the Error while executing powershell script. Can anyone please help me identify the error with hash table code
      Invoke-RestMethod : {“detail”:”Received JSON for the actions attribute does not match expected
      format.”,”error”:400,”errorCode”:”INVALID_JSON_ATTRIBUTE”,”parameters”:[“actions”],”reason”:”Bad Request”}
    • #238661
      Participant
      Topics: 0
      Replies: 30
      Points: 284
      Helping Hand
      Rank: Contributor

      Hello pk30,

      You need to add coma on line #11.

      You need to increase Depth parameter of ConvertTo-Json. Default value is 2 and it is too small for you nested objects structure.

      In your case it should be at least 4.

      $newrole2=$newrole | ConvertTo-Json -Depth 4
      
      

      Reference:

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-7

      Hope that helps.

       

      • This reply was modified 2 weeks, 4 days ago by AndySvints.
      • This reply was modified 2 weeks, 4 days ago by AndySvints.
Viewing 1 reply thread
  • You must be logged in to reply to this topic.