JSON Improvements

1C Developer team

14.04.2023 5 min

JSON.png

An increasing number of web services are moving to JSON messaging. To facilitate the work of application developers, we have decided to make the process of handling JSON as simple and convenient as possible.

Currently, there are 4 global context methods to read and write JSON: WriteJSON, WriteJSONDate, ReadJSON, ReadJSONDate. These methods offer extremely rich functionality and, as we see, it is redundant for most tasks of application developers. Still, the above methods are not available in web clients, while many integration tasks are handled through them.

In version 8.3.23, we added two new methods to the global context: WriteJSONValue and ReadJSONValue. The functionality of the new methods is "lighter" than the existing ones and should be sufficient for most integration tasks. While old methods ReadJSON and WriteJSON have to operate through objects ReadJSON and WriteJSON, our new methods offer much simpler syntax:

  • WriteJSONValue (<Value>) returns a string.

  • ReadJSONValue (<String>) returns Structure, Array, Number, String, Boolean, Undefined.

Both methods are available everywhere: thin client, web client, mobile client, server, thick client, external connection, mobile application (client), mobile application (server), and standalone mobile server. We do not deprecate the previous 4 methods, but we recommend using the new ones wherever possible. This, among other things, should make it possible to write versatile code that can run on all system components.

Here is an example. Assume we need to serialize a form's structure into a JSON string and then pass it to a web service:

{
       "LastName" "Johnson",
       "Name" "Peter"
       "MiddleName" "Matthew",
       "Phones": [
             "8-999-999-99-91",
             "8-999-999-99-92"
       ]
}

In the previous version, the code looks like this: 

&AtServer

Function GetJSONAtServer()

     Structure= NewStructure;

     Structure.Insert("LastName", "Johnson");

     Structure.Insert("Name", "Peter");

     Structure.Insert ("MiddleName", "Matthew");

     Phones = New Array;

     Phones.Add("8-999-999-99-91");

     Phones.Add("8-999-999-99-92");

     Structure.Insert("Phones",Phones);

     // getting JSON string

     WriteJSON = New WriteJSON;

     WriteJSON.SetString();

     WriteJSON(WriteJSON,Structure);

     Return WriteJSON.Close();

EndFunction

With the new functions utilized, the code can also run on a web client, and the procedure for getting JSON strings is shorter. Here you need just a single line instead of four:

&AtClient

Function WriteValueJSON Client ()

     Structure=NewStructure;

     Structure.Insert("LastName", "Johnson");

     Structure.Insert("Name", "Peter");

     Structure.Insert("MiddleName", "Matthew");

     Phones= New Array;

     Phones.Add("8-999-999-99-91");

     Phones.Add("8-999-999-99-92");

     Structure.Insert("Phones",Phones);

     // getting JSON string

     Return WriteJSONValue(Structure);

EndFunction

We expect new methods will facilitate the handling of many real-life integration tasks.

Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.