JSON format

JSON format

JSON format

JSON It is a text-based data exchange format, meaning it is easy to read for a person as well as a program. Its name corresponds to the English acronym of JavaScript Object Notation and is standardized as ECMA-404. Although it is a format very close in syntax to JavaScript, because it is very easy to process to encode object data or to obtain it, it is also used in many other programming languages ​​(C, C++, Java, Python...) as an alternative, for example, to XML, which has a similar objective but, because it includes more meta-information, requires more text and therefore takes up more space, consumes more bandwidth and requires more resources to encode and decode the information it contains. JSON.

Table of Contents

    The data of an object encoded in JSON They are enclosed in braces, the different properties included inside are separated by commas and the property names precede the value, from which they are separated by a colon.

    Outline of the syntax of an object in JSON format

    Property names must be enclosed in quotes using double quotes (although some parsers will support single quotes), and although some special characters are allowed for names (such as accent marks), it is not recommended to avoid the criteria of some analysis engines and to avoid conflicts when using property especially with dot(object.property) syntax

    In the example above you can see an object that has six properties with simple, numeric, boolean or text values; but in the format JSON, properties can also acquire as a value an object (including the "special object" null) or an array in addition to a text string (a text in quotes), a number (in different formats) or a boolean value (true or false ).

    As with language JavaScript (JSON is a subset of JavaScript) to express a matrix, its values ​​are enclosed in square brackets and separated by commas. The simplest case is illustrated in the example below; It is a one-dimensional array, a vector, composed of numerical values.

    En JavaScript It is not necessary that all the elements of an array be of the same type, you can mix, for example, text strings and numerical values. A vector, a one-dimensional array, can also be one of the elements of another array, allowing multidimensional arrays of variable lengths to be constructed. The following example shows an object with three properties: the first a two-dimensional array of fixed lengths, the second variable lengths, and the third formed by an array with values ​​of different types.

    The following example uses other objects as values ​​for the parent object's properties. There is no limit on the level of nesting so, in turn, objects that are property values ​​of the parent can also have other objects as their property values ​​and so on.

    Just as it happens with JavaScript, escape codes are used to include certain characters within a text string. The most generic way is to use unicode codes expressed with the escape bar, the letter u and four hexadecimal digits with the format "\u263A" to refer to ☺ for example.

    The standard characters, present in most languages ​​(similar to C) are the following

    • Recoil \b Custom code ASCII 8 (0x08)
    • Tabulator \t Custom code ASCII 9 (0x09) It is usually represented as HT (horizontal tabulator)
    • New line \n Custom code ASCII 10 (0x0A) It is usually represented as LF (line feed)
    • New page \f Custom code ASCII 12 (0x0C) Usually represented as FF (form feed)
    • ROI \r Custom code ASCII 13 (0x0D) It is usually represented as CR (carriage return)
    • Quotation marks \" Custom code ASCII 34 (0x22)
    • split bar \/ Custom code ASCII 57 (0x2F)
    • Backslash \\ Custom code ASCII 134 (0x5C)
    • unicode code \uXXXX

    Derived from C (and related to Unix and the like, such as GNU / Linux) the end of line is usually represented with \n and it is the one that must be chosen in JSON but it is interesting to remember that some operating systems prefer other alternatives. Windows usually represents with \r\n line endings and Mac OS (before OS \r

    Regarding the numerical format, the reference is also JavaScript. The decimal separator is the period, the dash (ASCII 45 0x2D) is used as a negative sign and the notation in exponential form (Scientific notation) uses E as indicator (which can be upper or lower case)

    As I said at the beginning, to use the data, which will have been obtained as a text, the different programming languages ​​have analysis and assignment functions. The following example shows how they would be used in JavaScript. To be able to use it from a console, the data assignment is included JSON To avoid possible errors depending on the ECMA standard used by the interpreter JavaScript the object assignment is made JSON in a single line. Edition 6 of 2016 (ECMA-262) supports multiline strings, edition 5 (until 2011) needs to add escape code \ and in the previous ones you have to solve it by hand or use a single line.

    A warning not to confuse new users in JavaScript. Inside the code JavaScript It is not necessary to use a text string to create an object, in the previous example it is used to simulate data arriving, for example, from a server. To assign an object to a variable, something like:

    That object could be converted to text, for example, with the function stringify as follows:

    The format JSON It is quite flexible but, mainly because it is text-based to be human readable, it has some drawbacks. The first is that it requires more memory and consumes more bandwidth than is essential. In the previous examples, a format has been used that aims to be more readable than economical, so they are not an example of savings, even so there are many elements of the format (braces, brackets, quotes...) that would continue to consume resources compared to the information represented. in raw.

    The second problem arises when dealing with purely binary data (for example, images). When dealing with small portions, it can be resolved with what was explained to resolve the formatting of special characters, but if it is about encoding information of certain dimensions, it will be necessary to embed it within the Format JSON using another text format. The most used encoding to resolve this aspect is Base64 since many languages ​​have libraries to convert information in one direction or another. If the data being manipulated is predominantly of this type and/or it is not necessary for a person to interpret the information without a program, it is worth considering whether the format JSON is the most suitable.

    Post Comment

    You May Have Missed