11

I am unable to create a variable on this request so I can later convert the variable to JSON using converttojson

{
    "update": {
        "comment": [
            {
                "add": {
                    "body": "Comment added when resolving issue"
                }
            }
        ]
    },

    "transition": {
        "id": "21"
    }
}

Tried the below

$jsonRequest = @{
    update= @{
        comment =@{
           add =@{
               body = "$Description"
            }
        }

    }
    transition =@{
        id = $TransactionID
    }
}

But get an output as below

{
    "transition":  {
                       "id":  1
                   },
    "update":  {
                   "comment":  {
                                   "add":  "System.Collections.Hashtable"
                               }
               }
}
1
  • How do I make it a list containing hashtable? Commented Jun 16, 2017 at 20:14

1 Answer 1

27

Comment" in your JSON is a list containing a hashtable, in your code it's a hashtable containing a hashtable.

This looks right by making it an array of one item:

$jsonRequest = [ordered]@{
    update= @{
        comment = @(
            @{
               add =@{
                   body = "$Description"
                }
            }
        )
    }
    transition = @{
        id = 21
    }
}

$jsonRequest | ConvertTo-Json -Depth 10

And I've made it '[ordered]' so the update and transition come out in the same order as your code, although that shouldn't really matter.

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

2 Comments

Thank you.. saved the day.
To clarify as well since I didn't see it mentioned; in your original output, it looks like you were using ConvertTo-Json without the -Depth parameter, which is why the add attribute just showed the type ("add": "System.Collections.Hashtable") when it went that deep (the default -Depth value is 2 & max is 100).

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.