"{ \"nodes\":
{ \"name\": \"Enron Announcements\", \"counts\": { \"name\": \"Enron Announcements\", \"role\": \"employee\", \"team_name\": \"Ufone\", \"oversees\": \"\", \"reports_to\": \"Zimin Lu,Lorna Brennan\", \"unique_threads\": \"366\" }, \"bridgeScore\": 0.0
},
{ \"name\": \"Enron worldwide\", \"counts\": { \"name\": \"Enron Announcements\", \"role\": \"employee\", \"team_name\": \"Ufone\", \"oversees\": \"\", \"reports_to\": \"Zimin Lu,Lorna Brennan\", \"unique_threads\": \"366\" }, \"bridgeScore\": 0.0
}, ...}
Above mentioned is what my JSON looks like. It is giving error that unexpected token { in Json. This error is thrown before name:Enron worldwide (before second one starts). How do I get rid of this error?
This is how I generated the JSON string:
for i in graph.nodes():
if "nan" not in str(i):
nodes.append({'name': str(i), 'counts': user_data[str(i)], 'bridgeScore':bridgeScore[str(i)]})
links = [{'source': u[0], 'target': u[1]}
for u in graph.edges()]
for u in graph.edges():
print(u)
# with open('graph.json', 'w') as f:
# return G
graph_json = json.dumps({'nodes': nodes, 'links': links},indent=2,)
graph_json = str(graph_json).replace("\n","")
graph_json = str(graph_json).replace("[","")
graph_json = str(graph_json).replace("]","")
graph_json = str(graph_json).replace("\\","")
with open('temp.json','w') as fp:
json.dump(graph_json , fp)
PS: json is generated through python and is to be rendered in JS
{ "nodes": {}, {} }
. Yes, this is invalid. You seem to want an array here.json
module that will take care of that.[
and]
as they are part of the integral structure of your json. Let thejson
module do the formatting for you, leave it alone. And don'tdumps
and thendump
it again.