ewitkows
ewitkows

Reputation: 3618

JSON for complex and simple data type

Im developing a WCF service that accepts JSON. My method signature accepts 2 parameters, a complex object and a simple type. For all intents and purposes below, assume "servicecredentials" has 2 properties, "username" and "password". I have valid JSON, but when I use a tool like postman I get the error "Expected to find an attribute with name 'type' and value 'object'. Found value 'array'.'"

How should this JSON be posted to the method?

<OperationContract()>
<WebInvoke(method:="POST")>
Function GetStuff(ByVal creds As servicecredentials, ByVal acctNum As String)

The JSON Im posting

[
    {
        "UserName": "someUSer",
        "Password": "p@ssw0Rd"
    },
    {
        "acctNum": "X12362"
    }
]

Upvotes: 0

Views: 210

Answers (1)

RogueBaneling
RogueBaneling

Reputation: 4471

The [] brackets indicate a JSON Array, the {} brackets indicate a JSON Object. If you encompass the array with the {} brackets it will be an object, which is what it seems to be looking for.

Example:

{
    "data": [
                {
                    "UserName": "someUSer",
                    "Password": "p@ssw0Rd"
                },
                {
                    "acctNum": "X12362"
                }
            ]
}

The exact internal structure of the JSON depends on how the method will process the data. The error is simply stating that the JSON is not encompassed by an object.

Upvotes: 1

Related Questions