Skip to content

Instantly share code, notes, and snippets.

@eshellman
Created March 23, 2015 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eshellman/b717d6b1b49498140218 to your computer and use it in GitHub Desktop.
Save eshellman/b717d6b1b49498140218 to your computer and use it in GitHub Desktop.
script I wrote to remove blank nodes from pg json-ld files
import yaml
import json
def unblank_node(node, bnodes):
if isinstance(node,dict):
if "@id" in node and node["@id"].startswith("_"):
if len(node)==1:
return bnodes[node["@id"]]
else:
return None
else:
newdict = {}
for key in node.keys():
newdict[key]= unblank_node(node[key], bnodes)
return newdict
elif isinstance(node,list):
newlist=[]
for item in node:
newnode= unblank_node(item, bnodes)
if newnode:
newlist.append(newnode)
return newlist
else:
return node
ld = json.loads(open("/Users/eric/github/local/metadata/pg20728.json","r").read())
graph = ld['@graph']
nodes = {}
for obj in graph:
if isinstance(obj,dict):
obj = obj.copy()
if "@id" in obj and obj["@id"].startswith("_"):
nodeid = obj["@id"]
node = nodes.get(nodeid,{})
del obj["@id"]
node.update(obj)
nodes[nodeid] = node
ld['@graph'] = unblank_node(graph,nodes)
#print test
#print yaml.safe_dump(test,default_flow_style=False)
open("/Users/eric/github/local/metadata/pg20728.ub.yaml","w+").write(yaml.safe_dump(ld,default_flow_style=False))
open("/Users/eric/github/local/metadata/pg20728.ub.json","w+").write(json.dumps(ld,indent=2, separators=(',', ': '), sort_keys=True))
print "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment