Deserialize Json to VL object

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:

7 Likes