What is json format. Programs for viewing and editing files with the JSON extension

The most common problem that prevents users from opening this file is an incorrectly assigned program. To fix this in Windows OS you need to click right click on the file, in context menu Hover your mouse over the "Open with" item and select "Select a program..." from the drop-down menu. As a result you will see a list installed programs on your computer, and you can choose the appropriate one. We also recommend checking the box next to “Use this application for all JSON files.”

Another problem that our users also encounter quite often is that the JSON file is corrupted. This situation can arise in many cases. For example: the file was downloaded incompletely as a result server errors, the file was damaged initially, etc. To fix this problem, use one of the recommendations:

  • Try to find required file in another source on the Internet. You may have luck finding a more suitable version. Example Google search: "File filetype:JSON" . Just replace the word "file" with the name you want;
  • Ask them to send you the original file again, it may have been damaged during transmission;

Surely you have ever heard of JSON. What is it? What can it do and how to use it?

In this tutorial we will cover the basics of JSON and cover the following points:

  • What is JSON?
  • What is JSON used for?
  • How to create a JSON string?
  • A simple example of a JSON string.
  • Let's compare JSON and XML.
  • How to work with JSON in JavaScript and PHP?
What is JSON?

JSON is a simple, text-based way to store and transmit structured data. With a simple syntax, you can easily store anything from a single number to strings, arrays, and objects in plain text. You can also link arrays and objects together to create complex data structures.

Once the JSON string is created, it is easy to send it to another application or to another location on the network because it is plain text.

JSON has the following advantages:

  • It's compact.
  • Its sentences are easy to read and compose by both humans and computers.
  • It can be easily converted into a data structure for most programming languages ​​(numbers, strings, booleans, arrays, etc.)
  • Many programming languages ​​have functions and libraries for reading and creating JSON structures.

The name JSON stands for JavaScript Object Notation (representation JavaScript objects). As the name suggests, it is based on a way of defining objects (much like creating associative arrays in other languages) and arrays.

What is JSON used for?

The most common common use of JSON is to send data from the server to the browser. Typically, JSON data is delivered using AJAX, which allows the browser and server to communicate without having to reload the page.

  • The user clicks on a product thumbnail in an online store.
  • JavaScript running on the browser generates an AJAX request to the PHP script running on the server, passing in the ID of the selected product.
  • The PHP script gets the product name, description, price and other information from the database. Then it composes a JSON string from the data and sends it to the browser.
  • JavaScript running on the browser receives the JSON string, decodes it, and displays the product information on the page for the user.
  • You can also use JSON to send data from the browser to the server by passing a JSON string as a parameter to GET or POST requests. But this method less common since data transfer via AJAX requests can be simplified. For example, the product ID may be included in the URL as part of a GET request.

    The jQuery library has several methods, such as getJSON() and parseJSON(), that make it easy to retrieve data using JSON through AJAX requests.

    How to create a JSON string?

    There are a few basic rules for creating a JSON string:

    • The JSON string contains either an array of values ​​or an object (an associative array of name/value pairs).
    • Array is enclosed in square brackets ([ and ]) and contains a comma-separated list of values.
    • An object is enclosed in curly braces (( and )) and contains a comma-separated list of name/value pairs.
    • name/value pair consists of the field name enclosed in double quotation marks, followed by a colon (:) and the field value.
    • Meaning in an array or object there can be:
      • Number (integer or floating point)
      • String (in double quotes)
      • Boolean value (true or false)
      • Another array (enclosed in square brackets)
      • Another object (enclosed in curly braces)
      • null value

    To include double quotes in a string, you need to use a backslash: \" . Just like in many programming languages, you can put control characters and hexadecimal codes to the string, preceded by a backslash. See the JSON website for details.

    Simple JSON string example

    Below is an example of ordering in JSON format:

    ( "orderID": 12345, "shopperName": "Vanya Ivanov", "shopperEmail": " [email protected]", "contents": [ ( "productID": 34, "productName": "Super product", "quantity": 1 ), ( "productID": 56, "productName": "Miracle product", "quantity": 3 ) ], "orderCompleted": true )

    Let's look at the line in detail:

    • We create an object using curly braces (( and )).
    • The object has several name/value pairs: "orderID": 12345 A property with the name "orderId" and an integer value 12345 "shopperName": "Vanya Ivanov" a property with the name "shopperName" and the string value "Vanya Ivanov" "shopperEmail": " [email protected]" A property named "shopperEmail" with a string value " [email protected]" "contents": [ ... ] A property named "contents" whose value is an array "orderCompleted": true A property named "orderCompleted" and a boolean true
    • There are 2 objects in the "contents" array representing individual items in the order. Each object contains 3 properties: productID , productName , and quantity .

    By the way, since JSON is based on declaring JavaScript objects, you can quickly and easily make the above JSON string a JavaScript object:

    var cart = ( "orderID": 12345, "shopperName": "Vanya Ivanov", "shopperEmail": " [email protected]", "contents": [ ( "productID": 34, "productName": "Super product", "quantity": 1 ), ( "productID": 56, "productName": "Miracle product", "quantity": 3 ) ], "orderCompleted": true );

    Comparison of JSON and XML

    In many ways, you can think of JSON as an alternative to XML, at least in the web application space. The concept of AJAX was originally based on the use of XML to transfer data between the server and the browser. But in recent years, JSON has become increasingly popular for transporting AJAX data.

    While XML is a proven technology that is used in a fair number of applications, JSON has the advantage of being a more compact and easier-to-recognize data format.

    This is what the above example object in XML would look like:

    orderID 12345 shopperName Vanya Ivanov shopperEmail [email protected] contents productID 34 productName Super product quantity 1 productID 56 productName Miracle product quantity 3 orderCompleted true

    The XML version is significantly larger. In reality it is 1128 characters long, while the JSON version is only 323 characters long. The XML version is also quite difficult to understand.

    Of course, this is a radical example. And it is possible to create a more compact XML record. But even it will be significantly longer than the JSON equivalent.

    Working with a JSON string in JavaScript

    JSON has a simple format, but creating a JSON string manually is quite tedious. Additionally, you often need to take a JSON string, convert its contents into a variable that can be used in code.

    Most programming languages ​​have tools to easily convert variables to JSON strings and vice versa.

    Creating a JSON string from a variable

    JavaScript has a built-in JSON.stringify() method that takes a variable and returns a JSON string representing its contents. For example, let's create a JavaScript object that contains the order information from our example, and then create a JSON string from it:

    var cart = ( "orderID": 12345, "shopperName": "Vanya Ivanov", "shopperEmail": " [email protected]", "contents": [ ( "productID": 34, "productName": "Super product", "quantity": 1 ), ( "productID": 56, "productName": "Miracle product", "quantity": 3 ) ], "orderCompleted": true ); alert (JSON.stringify(cart));

    This code will give:

    Note that the JSON.stringify() method returns a JSON string without spaces. It is more difficult to read, but it is more compact for transmission over the network.

    There are several ways to parse a JSON string in JavaScript, but the safest and most reliable is to use the built-in JSON.parse() method. It receives a JSON string and returns a JavaScript object or array that contains the data. For example:

    var jsonString = " \ ( \ "orderID": 12345, \ "shopperName": "Vanya Ivanov", \ "shopperEmail": " [email protected]", \ "contents": [ \ ( \ "productID": 34, \ "productName": "Super product", \ "quantity": 1 \), \ ( \ "productID": 56, \ "productName": "Miracle goods", \"quantity": 3\ ) \ ], \"orderCompleted": true \ ) \"; var cart = JSON.parse(jsonString); alert(cart.shopperEmail); alert(cart.contents.productName);

    We created a jsonString variable that contains the JSON string of our example order. Then we pass this line to the JSON.parse() method, which creates an object containing JSON data and stores it in the cart variable. All that remains is to check by displaying the properties of the shopperEmail object and productName of the contents array.

    As a result, we will get the following output:

    IN real application your JavaScript code will receive the order as a JSON string in the AJAX response from the server script, pass the string to the JSON.parse() method, and then use the data to display on the user's page.

    JSON.stringify() and JSON.parse() have other capabilities, such as using callback functions to custom convert certain data. Such options are very useful for converting various data into proper JavaScript objects.

    Working with a JSON string in PHP

    PHP, like JavaScript, has built-in functions for working with JSON strings.

    Creating a JSON string from a PHP variable

    The json_encode() function takes a PHP variable and returns a JSON string representing the contents of the variable. Here is our order example, written in PHP:

    This code returns exactly the same JSON string as in the JavaScript example:

    ("orderID":12345,"shopperName":"Vanya Ivanov","shopperEmail":" [email protected]","contents":[("productID":34,"productName":"Super product","quantity":1),("productID":56,"productName":"Miracle product","quantity": 3)],"orderCompleted":true)

    In a real application your PHP script will send this JSON string as part of the AJAX response to the browser, where the JavaScript code uses the JSON.parse() method to convert it back into a variable for display on the user's page.

    You can pass various flags as the second argument to the json_encode() function. With their help, you can change the principles of encoding the contents of variables into a JSON string.

    Create a variable from a JSON string

    To convert a JSON string into a PHP variable, use the json_decode() method. Let's replace our example for JavaScript with the JSON.parse() method with PHP code:

    As for JavaScript given the code will output:

    [email protected] Miracle product

    By default, the json_decode() function returns JSON objects as PHP objects. There are generic PHP objects of the stdClass class. That's why we use -> to access the properties of the object in the example above.

    If you need a JSON object as an associated PHP array, you need to pass true as the second argument to the json_decode() function. For example:

    $cart = json_decode($jsonString, true); echo $cart["shopperEmail"] . "
    "; echo $cart["contents"]["productName"] . "
    ";

    This code will produce the same output:

    [email protected] Miracle product

    You can also pass other arguments to the json_decode() function to specify the recursion depth and how to handle large integers.

    Conclusion

    Although JSON is easy to understand and use, it is a very useful and flexible tool for transferring data between applications and computers, especially when using AJAX. If you are planning to develop an AJAX application, then there is no doubt that JSON will become an essential tool in your workshop.

    JSON (JavaScript Object Notation) - a simple format data exchange, easy to read and write by both humans and computers. It is based on a subset of the JavaScript programming language, defined in the ECMA-262 3rd Edition - December 1999 standard. JSON- text format, is completely independent of the implementation language, but it uses conventions familiar to programmers of C-like languages ​​such as C, C++, C#, Java, JavaScript, Perl, Python and many others. These properties make JSON an ideal data interchange language.

    JSON is based on two data structures:

    • A collection of key/value pairs. IN different languages, this concept is implemented as an object, record, structure, dictionary, hash, named list, or associative array.
    • An ordered list of values. In most languages ​​this is implemented as array, vector, list or sequence.

    These are universal data structures. Almost all modern programming languages ​​support them in some form. It is logical to assume that a data format, independent of the programming language, should be based on these structures.

    In JSON notation it looks like this:

    An object- an unordered set of key/value pairs. An object begins with ( an opening curly brace and ends with a ) closing curly brace. Each name is followed by: a colon, key/value pairs separated by a comma.

    Array- an ordered collection of values. The array begins with a [ open square bracket and ends with ] a closing square bracket. Values ​​are separated by comma.


    Meaning May be line in double quotes, number, true , false , null , object or array. These structures can be nested.


    Line is a collection of zero or more Unicode characters, enclosed in double quotes, using the \ backslash as the escape character. The character is represented as a one-character string. Similar syntax is used in C and Java.


    Number represented in the same way as in C or Java, except that it is used only decimal system Reckoning.


    Spaces can be used between any tokens.

    Excluding some coding details, the above completely describes the language.


    JSON (JavaScript Object Notation) is a structured data format used to transmit data over the Internet.

    Essentially, the JSON format is a regular string.

    JSON syntax

    JSON syntax is quite small, it only includes a description of what the data being transferred looks like.

    JSON data types

    In JSON, data types can be divided into two categories: simple and complex.

    • string – text strings (usually they are simply called strings)
    • number - numbers
    • boolean – logical (Boolean) values
    • null

    Complex types include:

    • object - objects
    • array - arrays

    JSON syntax is borrowed from JavaScript, so it uses the same syntax as JavaScript to represent values ​​of simple and complex types.

    Simple values

    The simplest example JSON code – any value of a simple type:

    5 2.3 "Hello!" true null

    In JSON, strings must only be enclosed in double quotes. Using single quotes results in a syntax error.

    Objects

    A JSON object is a comma-separated list of zero or more properties (name: value pairs) enclosed in curly braces. Object property names must be enclosed in double quotes. Missing double quotes or using single quotes in a property name is an error. Properties can contain values ​​of any type (simple or complex):

    ("name": "Homer", "age": 40, "work": ("place": "Nuclear Plant", "location": "Springfield"))

    Arrays

    A JSON array is a comma-separated list of zero or more values, enclosed in square brackets. The array can contain values ​​of any type (simple or complex):

    [ ( "name": "Homer", "age": 40, "work": ( "place": "Nuclear Plant", "location": "Springfield"), "children": [ "Bart", "Lisa" ", "Maggie" ]), ( "name": "Marge", "age": 37, "work": ( "place": "Home", "location": "Springfield"), "children": [ "Bart", "Lisa", "Maggie" ] ) ]

    JSON or JavaScript Object Notation is a format that implements an unstructured textual representation of structured data based on the principle of key-value pairs and ordered lists. Although JSON originated with JavaScript, it is supported in most languages, either natively or with the help of special libraries. Typically Json is used to exchange information between web clients and the web server.

    Over the past 15 years, JSON has become a formal data exchange standard and is used virtually everywhere on the Internet. Today it is used by almost all web servers. Another reason for this popularity was the fact that many databases supported JSON. Modern relational databases such as PostgreSQL and MySQL now support storing and exporting data in JSON. Databases such as MongoDB and Neo4j also support JSON, although MongoDB uses a slightly modified version of JSON. In this article, we'll look at what JSON is, its advantages over XML, its disadvantages, and when it's best to use it.

    To understand why the JSON format is needed and how it works, you need practice. First let's look at this example:

    {
    "firstName": "Jonathan",
    “lastName”: “Freeman”,
    “loginCount”: 4,
    "isWriter": true,
    “worksWith”: [“Spantree Technology Group”, “InfoWorld”],
    “pets”: [
    {
    “name”: “Lilly”,
    “type”: “Raccoon”
    }
    ]
    }

    In this framework, we have clearly defined some of the attributes of a person. First, we determined the first name, last name, number of logins in the system, whether this person is a writer, a list of companies with which he works, and a list of pets. This or a similar structure can be transferred from the server to a web browser or mobile application, which can then do whatever it needs with this data, for example, display it or save it.

    JSON is a general data format with a minimum number of value types - strings, numbers, booleans (one or zero), lists, objects, and null. Even though JSON is a subset of JavaScript, most popular programming languages ​​have these types of data, making JSON a good candidate for passing data between programs written in different languages.

    Why should you use JSON?

    To understand the usefulness and importance of JSON, we need to understand a little about the history of interactivity on the Internet. In the early 2000s, the interactivity of websites began to change. At that time, the browser served only to display information, and the web server did all the work of preparing content for display. When the user clicked a button in the browser, the request was sent to the server, where it was collected and sent HTML page, ready for display. This mechanism was slow and ineffective. This required the browser to re-render everything on the page, even if a small portion of the data changed.

    At that time, transfers were charged based on the amount of data transferred, so the developers realized that reloading an entire page was very expensive and considered new technologies to improve the user interface. Then the ability to create web requests in background, which was added to Internet Explorer 5 turned out to be a fairly viable approach to loading data incrementally for display. Instead of reloading the page, clicking the button will simply issue a web request that will run in the background. Content will be updated as soon as it is loaded. It can be controlled from using JavaScript, a universal programming language for browsers.

    Initially the data was transferred to XML format, but it was difficult to use in JavaScript. JavaScript already had objects that were used to represent data in the language, so Douglas Crockford took the JS object syntax and used it as the specification for a new data interchange format called JSON. This format was much easier to read and parse in a JavaScript browser. Soon, developers began using JSON instead of XML.

    JSON is now the de facto standard for transferring data between server and client. mobile applications and even internal system services.

    JSON vs XML

    As I said above, the main alternative to JSON was and is XML. However, XML is becoming less common in new systems. And it's very easy to understand why. Below is an example of writing the data you saw above in Json via XML:


    Jonathan
    Freeman
    4
    true

    Spantree Technology Group
    InfoWorld

    Lilly
    Raccoon


    In addition to the redundancy of the code, which essentially took up twice as much space to write the data, XML also introduces some ambiguity when analyzing the data structure. Converting XML to a JavaScript object can take tens to hundreds of lines of code and requires fine tuning for each analyzed object. Converting JSON to a JavaScript object is done in one line and does not require any prior knowledge about the object being parsed.

    JSON Limitations

    Although JSON is a relatively compressed and flexible data format that is easy to work with in many programming languages, it does have some disadvantages. Here are some restrictions:

    • No structure. On the one hand, this means that you have complete flexibility to present the data in any way you want. On the other hand, you can easily store unstructured data.
    • Only one type of numbers. IEEE-754 floating point and double precision format is supported. That's quite a lot, but you can't use the variety of numeric types that other languages ​​have.
    • No date type. developers must use string representations of dates, which can cause formatting inconsistencies. Or use as the date the number of milliseconds that have passed since the beginning of the Unix epoch (January 1, 1970).
    • No comments - You won't be able to annotate fields that require them directly in the code.
    • Verbose - While JSON is less verbose than XML, it is not the most concise data interchange format. For high-end or specialized services, you'll want to use more efficient formats.
    When should you use JSON?

    If you are developing software that interacts with a browser or native mobile app, you're better off using JSON. The use of XML is deprecated. For communication between servers, JSON may not be very efficient and it is better to use a serialization framework similar to Apache Avro or Apache Thrift. Even here, JSON is not a bad choice and can give you everything you need. But there is no exact answer what to choose.

    If you are using bases MySQL data, your program will depend heavily on what is being done in the database. In relational databases that support JSON, it is considered good practice to use as little of it as possible. Relational databases were designed for data with a specific schema. Although most of them now support the JSON data format, the performance of working with it will be significantly lower.

    conclusions

    JSON is a data format aimed primarily at sending data between web servers and browsers or mobile applications. The format has a simple design and flexibility, it is easy to read and understand, and it is easy to work with such data in most programming languages. The lack of a strict schema allows for flexibility in the format, but this flexibility can sometimes make the data difficult to read and understand.

    You may have a hard time working with JSON in a strictly typed languages, such as Scala or Elm, but the widespread adoption of the format suggests that there are utilities and libraries that can help even with complex problems. Now you know what json is and how to use it.