Deserialize Json to VL object

I am dealing with a JSON stream that has a rather complicated array of objects. There are twice- and sometimes triple-nested objects. Therefore, DeserializeJson(MessagePack) fails to deserialize it.
A workaround is that I parse it as xml and enable “Array Attribute”, then back to Json and deserializing it to VLObject.

is there a level of nested objects where the Deserializer would fail?

Here is the json sample

{
“ok”: true,
“result”: [
{
“update_id”: 111546405,
“message”: {
“message_id”: 107,
“from”: {
“id”: 11111011,
“is_bot”: false,
“first_name”: “john”,
“username”: “johnjohn”,
“language_code”: “en”
},
“chat”: {
“id”: 111119011,
“first_name”: “john”,
“username”: “johnjohn”,
“type”: “private”
},
“date”: 12116935064,
“text”: “First”
}
},
{
“update_id”: 60127606,
“message”: {
“message_id”: 108,
“from”: {
“id”: 115128819011,
“is_bot”: false,
“first_name”: “john”,
“username”: “johnjohn”,
“language_code”: “en”
},
“chat”: {
“id”: 11598899011,
“first_name”: “john”,
“username”: “johnjohn”,
“type”: “private”
},
“date”: 1987935076,
“text”: “Second one”
}
},
{
“update_id”: 601888766407,
“message”: {
“message_id”: 109,
“from”: {
“id”: 1198006219011,
“is_bot”: false,
“first_name”: “john”,
“username”: “johnjohn”,
“language_code”: “en”
},
“chat”: {
“id”: 186755219011,
“first_name”: “john”,
“username”: “johnjohn”,
“type”: “private”
},
“date”: 17198780,
“text”: “Another one”
}
},
{
“update_id”: 60158698764408,
“message”: {
“message_id”: 110,
“from”: {
“id”: 11521901s341,
“is_bot”: false,
“first_name”: “john”,
“username”: “johnjohn”,
“language_code”: “en”
},
“chat”: {
“id”: 11754165011,
“first_name”: “johnjohn”,
“username”: “johnjohn”,
“type”: “private”
},
“date”: 1716935109,
“photo”: [
{
“file_id”: “AgACAjkgzAAxkBAANulknlZZxfP8aJ-3MHKm1FxjcLRp5_oAAplkhuizztRunljksNFSZLSiABAAMCAANzAAM1BA”,
“file_unique_id”: “AQAssbsccxGjhikkJ4”,
“file_size”: 951,
“width”: 90,
“height”: 50
},
{
“file_id”: “AgACAgQAAvsvBAANuZlZZxfP8aJ-3MHKm1svslknp5_oAAp3HMssDKLFSaOgqs8ZLMCAANtAAM1BA”,
“file_unique_id”: “AQADnlöskosVJy”,
“file_size”: 9994,
“width”: 320,
“height”: 178
},
{
“file_id”: “AgACAkjhlZZxfP8aJ-3MHKm1FxjcLRp5_oAAp3HMRuDKL,m iuAAMCAAN4AAM1BA”,
“file_unique_id”: “lknlkmvsssVJ9”,
“file_size”: 38256,
“width”: 800,
“height”: 444
},
{
“file_id”: “AgAClklZZxfP8aJ-3MHKm1FxjcLRp5_oAAp3HMkjbjkSiABAAMCAAN5AAM1BA”,
“file_unique_id”: “AQAmnppsüosVJ-”,
“file_size”: 68741,
“width”: 1280,
“height”: 710
}
],
“caption”: “Image with caption”
}
}
]
}

here is an alternative to parsing json that i find quite comfortable, requires Visual Studio (not VS Code!) though:

  • in your patch add a C# File choosing the “Satic Utils” template in the wizard
  • make sure the C# project opens in Visual Studio (not VS Code!)
  • select all the template code in the .cs file and delete it
  • put a sample of your .json text in the clipboard
  • in VS go to Edit → Paste Special → Paste JSON as Classes
  • save the .cs file and go back to vvvv
  • find the generated nodes in the nodebrowser
  • in your .vl document reference System.Text.Json (via GAC or nuget)
  • use the node Deserialize [System.Text.Json.JsonSerializer] to parse your json string
  • use the generated nodes to access the elements of your data

while this may sound rather complex, here are the benefits:

  • you get the full class structure for free, instead of manual parsing using xpath or similar
  • you can now use all of the endless parsing options of the System.Text.Json library, see How to read JSON as .NET objects
  • you can now version the generated C# code with your project and should the json change, simply regenerate the classes and get a proper diff

your example can look something like this:
grafik

7 Likes