Reputation: 2797
Does JSON require a root element as in XML case. As far as I know this is a valid JSON string.
{
"email":[
{
"type":"home",
"name":"[email protected]"
},
{
"type":"work",
"name":"[email protected]"
}
]
}
I need to convert JSON to XML an vice versa. However although the above is valid JSON when I convert it to XML it is not valid? Am I missing something or this is normal?
Upvotes: 28
Views: 63094
Reputation: 163322
The outermost level of a JSON document is either an "object" (curly braces) or an "array" (square brackets).
Any software that converts JSON to XML has to reconcile the fact that they are different data models with different rules. Different conversion tools handle these differences in different ways.
UPDATE (2021-09-03): As noted in the comments, subsequent iterations on the JSON specification allow the outermost level to be a string, number, boolean, or null.
Upvotes: 39
Reputation: 20852
According to the modified Backus-Naur-Form on the right side pane of http://json.org/ the root element of a JSON data structure can be any of these seven types/values:
Object
Array
String
Number
true
false
null
So all of the following examples are valid JSON root elements:
{
"name": "Jpsy",
"age": 99
}
[ 1, 2, "three", 4, 5 ]
"abcdefg"
123.45
true
false
null
Upvotes: 17
Reputation: 3833
This is normal, json and xml don't have the same rules. You can transfrom your root brackets "{" and "}" into a root element to be sure to don't have conversion problems
Upvotes: 2