Understanding the JSON Structure - Attributes and Values

Something is bothering me. I used JSON in several of my apps / websites, we all like it! However, today something entered my head that I had never thought about. Take a look at the following example (this is taken from http://json.org/example.html ):

{"widget": { "debug": "on", "window": { "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 }, "image": { "src": "Images/Sun.png", "name": "sun1", "hOffset": 250, "vOffset": 250, "alignment": "center" }, "text": { "data": "Click Here", "size": 36, "style": "bold", "name": "text1", "hOffset": 250, "vOffset": 100, "alignment": "center", "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" } }} 

now, if it was expressed as XML: we would have the following:

 <widget> <debug>on</debug> <window title="Sample Konfabulator Widget"> <name>main_window</name> <width>500</width> <height>500</height> </window> <image src="Images/Sun.png" name="sun1"> <hOffset>250</hOffset> <vOffset>250</vOffset> <alignment>center</alignment> </image> <text data="Click Here" size="36" style="bold"> <name>text1</name> <hOffset>250</hOffset> <vOffset>100</vOffset> <alignment>center</alignment> <onMouseUp> sun1.opacity = (sun1.opacity / 100) * 90; </onMouseUp> </text> </widget> 

When looking at converting JSON to XML, I wonder if there is any way you can tell if a pair ("key": "value") is an attribute of a tag. For example, in our JSON above

 "window": { "title": "Sample Konfabulator Widget", "name": "main_window", 

title is represented as an attribute of the window tag, while the name is a tag in its own right, however it is not expressed anywhere (what I see).

 <window title="Sample Konfabulator Widget"> <name>main_window</name> 

Why it is not displayed as:

 <window> <title>Sample Konfabulator Widget</title> <name>main_window</name> 

Is this a way, can I indicate if a pair is an attribute of a parent tag or a child tag on its own? Sorry if my wording is bad or I am not explaining myself well.

+7
source share
7 answers

It depends on your converter.

Json does not conform to XML 1-1.

Simply put, json is just a way of representing dictionaries and arrays mutated nested.

+2
source

Some people usually use a regular property called "@attributes", giving it the special meaning of an "attribute container", but this name has no special meaning for the JSON specifications themselves.

To answer your question, there is no way to distinguish between key: value pairs and attributes, simply because json does not support attributes.

This question usually comes from people who forget that M in XML means “markup,” which is a way of plain text and adding markup to create structured text. I think that the misunderstanding of the codes from JSON promotion is a low-fat alternative to XML , but in fact XML is a markup language, and JSON is data exchange format. No one is an alternative to the other.

Mixed content is one context where XML shows its power.

 <p>What a <b>mess</b> we're in.</p> 

Trying to express this as JSON, you will realize that it was not intended for this work.

XML, on the other hand, can be very useful as a data exchanger, but that’s not what it was designed for or where its main strengths are.

+2
source

Some converters use default keywords, such as "title" and "src", which translate as an attribute instead of a field. Some converters do not.

+1
source

Not inside JSON. I assume that you used some kind of automatic way to convert to XML from JSON; Depending on the program that is doing the conversion, there may be annotations that you can use. See Also, for example. http://en.wikipedia.org/wiki/JSON , which provides two XML examples for the same JSON.

There is no formal specification (which I know) about how you "should" display JSON in XML. Both are formalized, structured ways of presenting data, but leave specific parts of how you present data to you. So it depends on how you want to present the data, and on what your application may or may not deal with.

+1
source

I just read this exact example on a json site and thought: “As there is a name on earth, an attribute there doesn’t mean anything that it should. I would assume that the example itself is not mistaken, but I don’t know why someone hasn't suggested a simple answer?

0
source

I think this is (somewhat) handled by the fact that "title" and "description" (and "default") are considered keywords in JSON. JSON does not really handle attributes this way. I think the only way to do this is to add another encapsulating object to separate the “header” from the other entries. I think the efforts of JSON to make the "title" and "description" special are an attempt to handle this. (Not a great answer, I know ....)

https://spacetelescope.imtqy.com/understanding-json-schema/reference/generic.html

0
source

The title object in widget / window will be generated into a new element. It will never fall into the attribute of another element. Very sure of that.

0
source

All Articles