Johannes
Johannes

Reputation: 6717

Is there a notation for a JSON type in a JSON string

I am thinking about using JSON as a way to communicate information inside my programm in a way suggested in this talk.

As i read the JavaScript Object Notation i see no way of noting my objecttype.

Suppose i communicate the string { "val" : 5 }, how would i know what that string was for.

I would like to send the string error = { "val" : 5 } and measurement = { "val" : 5 }. but as i read it this would not be valid JSON notation.

Is the solution always something like { "type" : "error", "val" : 5 } or am i missing some bigger concept in JavaScript Object Notation.

EDIT: ops - did not do JSON in my examples, fixed that

Upvotes: 0

Views: 32

Answers (2)

meda
meda

Reputation: 45500

{
    "type": "error",
    "val": 5
}

That's the proper way to format your JSON

If you have different type of values, then you will be able to have an array looking like this:

[
    {
        "type": "error",
        "val": 5
    },
    {
        "type": "measurement",
        "val": 45
    }
]

Upvotes: 1

Buck Doyle
Buck Doyle

Reputation: 6397

In JSON (and Javascript in general), the key name identifies the type of the value. A JSON-like version of your examples:

{
  'error': 5,
  'measurement': 5
}

Upvotes: 1

Related Questions