Python dotGraph failed to parse JSON

Author: DeDiamondProCreated Dec 5, 2024Updated Feb 14, 2025

Hi,

I was trying to use the dotGraph type with python, unfortunately it seems like the JSON parser is unable to parse it. Code to replicate, text is taken from your playground example:

python
import json

text = "\ndigraph G {\n    subgraph cluster_0 {\n      style=filled;\n      color=lightgrey;\n      node [style=filled,color=white];\n      a0 -> a1 -> a2 -> a3;\n      label = \"process #1\";\n    }\n  \n    subgraph cluster_1 {\n      node [style=filled];\n      b0 -> b1 -> b2 -> b3;\n      label = \"process #2\";\n      color=blue\n    }\n    start -> a0;\n    start -> b0;\n    a1 -> b3;\n    b2 -> a3;\n    a3 -> a0;\n    a3 -> end;\n    b3 -> end;\n  \n    start [shape=Mdiamond];\n    end [shape=Msquare];\n}\n"
value = json.dumps({"kind": {"dotGraph": True}, "text": text})

print(value)

This fails to parse with the following error: image

I think this is caused by the fact that the JSON it is trying to parse seems to be double escaped, for example I would expect it to be escaped like this: label = \"process #1\", this is also what happens if I print the string, but instead the string it is trying to parse contains label = \\"process #1\\", which causes the " to not be escaped and I think that is what is causing parsing to fail.

Source: hediet/vscode-debug-visualizer