Appendix
  • 01 Mar 2022
  • 2 Minutes to read
  • PDF

Appendix

  • PDF

Article summary

JSON object format

When using JSON, the JSON string will automatically be parsed to an object with the same structure as the string, since JSON was designed to be a textual representation of a JavaScript object. A JSON text and its JS equivalent can be seen in the below snippets:

"data": {
    "info": {
        "Tag Path": "/RestApiClient/Tag1"
        "ts": 1550569887810,
        "good": true
    },
    "values ": [5, 23, 12]
    }
}
data: {
    info: {
        "Tag Path": "/RestApiClient/Tag1"
        ts: 1550569887810,
        good: true
    },
    values: [5, 23, 12]
    }
}


As an example, the timestamp can be retrieved by accessing data.info.ts, while the value array can be retrieved by using data.values (which can then be iterated in order to extract each value).

XML object format

When using XML, the XML string will automatically be parsed to a JavaScript object using several rules and special characters to generate the object. Parsing depends on the tags and their structure, but in general, the following rules apply:

<main>
<datapoint ts="1550569887810">
    <tag path="/RestApiClient/Tag1"/>
    <good>true</good>
    <values>
        <sample value="5"/>
        <sample value="23"/>
        <sample value="12"/>
    </values>
</datapoint>
</main>


  • Each tag (except the root) is parsed as an array of one or more elements. As such, an index must be used when traversing the nested tags. For example, the <good> tag can be retrieved by using main.datapoint[0].good.
  • Attributes of a tag can be accessed by using the $ character after navigating to the specific tag. For example, the path attribute of the <tag> tag can be accessed by using main.datapoint[0].tag[0].$.path
  • The text value of a tag can be accessed directly if the tag has no attributes. If the tag does have attributes, it can be accessed using the special character _. In this case, the text value for the <good> tag can be accessed directly, since the <good> tag has no attributes. This is done by using main.datapoint[0].good[0]
  • When a tag contains several tags with the same name, these tags will be parsed as an array contained in the parent tag. This can be seen with the <sample> tags contained in the <values> tag. For example, to access the second sample (with value 23), the following example must be used: main.datapoint[0].values[0].sample[2].$.value

Using these rules, the following JavaScript object is generated after parsing the XML:

{
    main: {
        datapoint: [
            {
                $: {
                    ts: "1550569887810"
                },
                tag: [
                    {
                        $: {
                            path: "/RestApiClient/Tag1"
                        }
                    }
                ],
                good: [
                    "true"
                ],
                values: [
                    {
                        sample: [
                            {
                                $: {
                                    "value": "5"
                                }
                            },
                            {
                                $: {
                                    "value": "23"
                                }
                            },
                            {
                                $: {
                                    "value": "12"
                                }
                            }
                        ]
                    }
                ]
            }
        ]
    }
}



Was this article helpful?

What's Next