Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is it possible to express a JSON graph (as opposed to a simple tree)? I didn't think it was.


I think the author means Diagram instead of graph as data structure


That is a graph. It has vertices and edges. Any tree or hierarchical structure can be expressed this way.


Of course the display representation on the tool is indeed a graph, but some people might wonder how one can represent a multi-parent node in a JSON tree or have some cyclic structure, which of course is not possible and cause confusion


With JSON pointers [0] this is possible. For example OpenAPI allows them. Can be a pretty useful extension.

[0] https://www.rfc-editor.org/rfc/rfc6901


All JSON would be an acyclic graph, as JSON does not support cyclically linked elements (that I'm aware of). You'd represent encapsulated JSON objects with a structure like a 'polyforest tree'.

If cyclicallity is needed specifically, cyclic graphs do exist, but it was my impression that JSON doesn't have these, or I'm unable to imagine how that would work. While two fields may have the same 'value' in a JSON object, I don't know how I could express cyclicallity using normal JSON syntax to say that two identical values would be linked cyclically.


you are correct, you can't have cycles in json. you can invent conventions for representing cycles, but can't express them natively. i did write a toJS stringifier that handles cycles, but it outputs JS not JSON


Ah, right. I thought they had some adjacency list format or something.


Does JSON Crack support a node having multiple parents?


What is a JSON graph?


A general graph is a tree without the restriction that a node can have at most one parent.


Correct.

So how is JSON a graph? Or maybe the answer is just, No. Not generally. I'm confused by your question.


You're just repeating what I originally asked?


Fine, let me be clear then.

No. JSON is not a graph.

Any visualization of a graph from JSON would be a specific encoding of a graph (probably using IDs) which is not generally interpreted as a graph.


Tree is a type of graph, though.


Each object is a node.

{ Left:{}, Right:{} }

Has a tree with 3 nodes

If you don't parse the JSON, you tend to traverse it as a graph


trees are graphs, so there is no problem!




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: