2 years ago

#42419

test-img

MikeB2019x

Create a Postgres Schema from a JSON file

I have a pretty simple json file that represents a network (see below). I would like to create a postgres schema from the file so that would allow me to store the network structure in a postgres db. My searches online seem to be related to storing json in a field but not breaking down the json in to a tabular structure.

My approach so far (code below) is to use pandas to convert the json to a table that I can then just import in to postgres. It seems really kludgy so is there a better way because I would like to scale to more complex networks? It seems to me that this might be a question of better ways to convert a python dict to a table?

import networkx as nx
import pandas as pd
from random import randint


# Create a directed graph
G = nx.circular_ladder_graph(5).to_directed()
G.name = 'test'
# Randomize edge weights
nx.set_edge_attributes(G, {e: {'weight': randint(1, 9)} for e in G.edges})

data = nx.node_link_data(G)

test = pd.DataFrame.from_dict(data, orient='index').transpose()

pd.json_normalize(test.nodes[0]).merge(pd.json_normalize(
    test.links[0]), left_index=True, right_index=True, how='right').merge(
    test[['directed', 'multigraph', 'graph']], left_index=True, 
    right_index=True, how='left')[['directed', 'multigraph', 
                                           'graph', 'id', 'weight', 'source', 'target']]

json

postgresql

schema

digraphs

0 Answers

Your Answer

Accepted video resources