Structure, Array and ValueTable to JSON in 1C: both directions
Checkcraft is a desktop app for checking APIs and logs without code, with a free edition. Download, no sign-up →
WriteJSON understands Structure, Map, Array and simple types, but not a ValueTable directly. So the table is first turned into an array of structures, and when reading it is rebuilt from the array. Below are ready functions for both directions and the pitfalls with types and dates.
Which 1C types go to JSON
Written directly: Structure and Map with string keys become an object, Array becomes an array, String, Number, Boolean and Undefined becomes null, Date with serialization settings. ValueTable, references to catalogs and documents and ValueStorage are not written directly: convert them to simple types first.
ValueTable to JSON
Walk the table rows, create a Structure with the columns for each row, then write the resulting Array. Column names become JSON keys. Replace references with a code, a name or a UUID, otherwise the receiver cannot understand the value.

JSON array to ValueTable
Read the JSON into an Array of Maps, create a ValueTable with the needed columns and types, and add a row for each array element, filling columns by key. Set types explicitly: JSON numbers arrive as Number and dates as strings unless listed when reading.
ValueTable to a JSON string
Функция ТаблицаВJSON(Таблица)
Массив = Новый Массив;
Колонки = Новый Массив;
Для Каждого Колонка Из Таблица.Колонки Цикл
Колонки.Добавить(Колонка.Имя);
КонецЦикла;
Для Каждого Строка Из Таблица Цикл
Элемент = Новый Структура;
Для Каждого Имя Из Колонки Цикл
Элемент.Вставить(Имя, Строка[Имя]);
КонецЦикла;
Массив.Добавить(Элемент);
КонецЦикла;
Запись = Новый ЗаписьJSON;
Запись.УстановитьСтроку();
ЗаписатьJSON(Запись, Массив);
Возврат Запись.Закрыть();
КонецФункцииIf columns hold references, replace them with a code or UUID before writing, for example String(Ref.UUID()). Otherwise WriteJSON fails with an unsupported type error.

JSON array to a ValueTable
Функция JSONВТаблицу(СтрокаJSON)
Чтение = Новый ЧтениеJSON;
Чтение.УстановитьСтроку(СтрокаJSON);
Массив = ПрочитатьJSON(Чтение, Истина); // массив соответствий
Чтение.Закрыть();
Таблица = Новый ТаблицаЗначений;
Таблица.Колонки.Добавить("sku", Новый ОписаниеТипов("Строка"));
Таблица.Колонки.Добавить("qty", Новый ОписаниеТипов("Число"));
Для Каждого Элемент Из Массив Цикл
Строка = Таблица.Добавить();
Строка.sku = Элемент.Получить("sku");
Строка.qty = Элемент.Получить("qty");
КонецЦикла;
Возврат Таблица;
КонецФункцииColumns and types are set explicitly. The table then does not depend on which keys arrived in a given response, and extra partner fields do not break processing.
What becomes what
| 1C | JSON | Back to 1C |
|---|---|---|
| Structure | object | Structure or Map |
| Array | array | Array |
| ValueTable | not directly, array of objects via a function | Array, then a table by hand |
| Reference | not directly, code or UUID string | Lookup by code or UUID |
| Date | ISO string with settings | String, or Date when fields are listed |
Reading and writing JSON in 1C is covered in JSON in 1C.
More on 1C
Section overview and all guides: 1C integration via API: options, architecture and examples.
- Data exchange in 1C: methods, formats, setup and checks
- Integrating 1C with a website and online store: options and pitfalls
- Automated testing in 1C: Vanessa Automation, YAxUnit and integration checks
- REST API in 1C: HTTP services, OData and calling external APIs
- 1C HTTP services: create, publish, test and debug with examples
Frequently asked questions
How do I convert a ValueTable to JSON in 1C?
Walk the rows, build a Structure per row, collect an Array and write it with WriteJSON. WriteJSON does not accept a ValueTable directly.
How do I convert JSON to a Structure?
ReadJSON(reader, False) returns a Structure; use a Map (True) for keys with dashes or leading digits.
Repeat this on your own API
Checkcraft is a desktop app that saves API response checks without code and shows what did not match. Start with a sample, then use your own sanitized data. Free runs on Windows and Linux with no time limit.
Download FreeDownload and first runNo sign-up or email needed, the archive is ready in a couple of minutes. See the edition limits.
References
Start with the linked example and compare its result with your own test requirement.