Java – Do JSON value types need to be strictly defined?

Do JSON value types need to be strictly defined?… here is a solution to the problem.

Do JSON value types need to be strictly defined?

I’ve come across an API that returns different types of values for “fieldValue” as follows:

{
    "id" : 123,
    "fieldType" : "text",
    "fieldValue" : "some test"
}
{
    "id" : 456,
    "fieldType" : "checkbox",
    "fieldValue" : 
    [
        {
            "checkboxId" : 1,
            "name" : "Homer"
        },
        {
            "checkboxId" : 2, 
            "name" : "Marge"
        }
    ]
}
{
    "id" : 789,
    "fieldType" : "Select",
    "fieldValue" : {
        "selectId" : 3,
        "value" : "Lisa"
    }
}

I’m using GSON and it doesn’t like the fact that “fieldValue” can be a string or an object or an array. I’ve written a custom deserializer to parse it.
My question is does the JSON spec allow JSON objects to have loosely defined value types, meaning that the fieldValue type can be a string, an array of objects, or an object?

Related Problems and Solutions