It looks like you have a list of paths where the parts overlap.
First remove the duplicate edges:
SELECT DISTINCT node
, lag(node) OVER (PARTITION BY path_id ORDER BY id) AS parent
FROM tbl
ORDER BY parent NULLS FIRST, node;
parentis NULL for the root of the node. You can remove this "non-edge" from the result.
Then,, generate a JSON-object for this treeyou can use json_agg():
SELECT json_agg(sub) AS array_of_edges
FROM (
SELECT DISTINCT node
, lag(node) OVER (PARTITION BY path_id ORDER BY id) AS parent
FROM tbl
ORDER BY parent NULLS FIRST, node
) sub;
SQL Fiddle
source
share