Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding JSON structure - attributes and values

Something is bothering me. I've used JSON in a few of my apps/websites, we all love it! However something entered my head today which I've 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 this 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 JSON to XML transformation I'm wondering if is there is any way that you can distinguish if a pair ("key":"value") are a 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 whilst name is a tag in it's own right, however this isn't expressed anywhere (that I can see).

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

Why isn't this displayed as:

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

Is their a way I can specify if a pair are an attribute of the parent tag or a child tag in their own right? Sorry if my wording is bad or if I'm not explaining myself well.

like image 494
Mike Sav Avatar asked Dec 05 '11 10:12

Mike Sav


People also ask

What are the attributes in JSON?

JSON does not have a similar notion to attributes as in XML. Because of this, attributes must be represented in JSON in a different manner. Here, we see that JSON properties just contain objects or values - no attributes. NIEM attributes are represented as regular properties.

How would you describe the structure of a JSON?

JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values. JSON defines seven value types: string, number, object, array, true, false, and null. The following example shows JSON data for a sample object that contains name-value pairs.

What is [] and {} in JSON?

' { } ' used for Object and ' [] ' is used for Array in json.

What are the parts of JSON called?

The two primary parts that make up JSON are keys and values. Together they make a key/value pair. Key: A key is always a string enclosed in quotation marks. Value: A value can be a string, number, boolean expression, array, or object.


1 Answers

Some people tend to use a regular property named "@attributes", giving it the special meaning of "attributes container", but that name does not have any special meaning for the JSON specifications themselves.

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

This kind of question usually comes from people who forget that the M in XML stands for "markup", which is a way of taking plain text and adding markup to create structured text. I guess that the misunderstanding cames from promoting JSON as a fat-free alternative to XML, while actually XML is a markup language, and JSON is a data interchange format. No one is really an alternative to the other.

The mixed content is one of the contexts where XML shows its power.

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

Trying to express that as JSON, you'll realize that it wasn't designed for the that job.

On the other hand, XML may be quite useful as data interchanger, but that's not what it was designed for or where it's main strengths are.

like image 67
Demis Palma ツ Avatar answered Oct 15 '22 03:10

Demis Palma ツ