Created
November 19, 2019 15:24
-
-
Save prashant-shahi/610c9e2f2f3e04a2045b5ae715c63af3 to your computer and use it in GitHub Desktop.
Script to add Dgraph meta data to a JSON dataset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type User { | |
id: string | |
name: string | |
profile: string | |
picture_link: string | |
city: string | |
edu: string | |
work: string | |
cityn: [City] | |
edun: [College] | |
workn: [Company] | |
} | |
type City { | |
id: string | |
name: string | |
} | |
type College { | |
id: string | |
name: string | |
} | |
type Company { | |
id: string | |
name: string | |
} | |
id : string @index(exact) . | |
name : string @index(term) . | |
profile : string . | |
picture_link : string . | |
city : string @index(term) . | |
edu : string @index(term) . | |
work : string @index(term) . | |
edun : [uid] @reverse @count . | |
workn : [uid] @reverse @count . | |
cityn : [uid] @reverse @count . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{ | |
"id": "100023", | |
"name": "Faiz Ali", | |
"profile": "https://www.facebook.com/profile.php?id=100023", | |
"picture_link": "https://scontent.fisb1-1.fna.fbcdn.net/v/t1.0-1/p40x40/30440726_18089952_o.jpg?_nc_cat=108&_nc_oc=AQmprMoO4Nk65PO95Y1SQjmOsqofdKDV80uVmdkg0aC6IEIpmiWWSJn7oHpNqLuyxzg&_nc_ht=scontent.fisb1-1.fna&oh=512fe306a0d4cafcb480e097c8bd04d4&oe=5E41EAD0", | |
"city": "", | |
"edu": "", | |
"work": "", | |
"cityn": [], | |
"edun": [], | |
"workn": [] | |
}, { | |
"id": "zeeshaaawn.qmughal", | |
"name": "Zen Sahal", | |
"profile": "https://www.facebook.com/zghal?fref=pb&__tn__=%2Cd-a-R&eid=ARD00TXVW6zsNuDDsHw_dBTXq9ngx0nPKbHj&hc_location=profile_browser", | |
"picture_link": "https://scontent.fisb1-1.fna.fbcdn.net/v/t1.0-1/p40x40/7235887_457342973969956864_o.jpg?_nc_cat=110&_nc_oc=AQk0_2dMsm_5fJqb7W3Za0HQt-t_Vbo_o02iWYIv8_BSLJdIP-V3Xv_x3Th04aMiP0o&_nc_ht=scontent.fisb1-1.fna&oh=eed9858a9a1f256aba3df5c4d364b841&oe=5E4F0936", | |
"city": "", | |
"edu": "F.G College for men", | |
"work": "Private", | |
"cityn": [], | |
"edun": [{ | |
"id": "f.gcollegeformenh-9i", | |
"name": "F.G College for men " | |
}], | |
"workn": [{ | |
"id": "private", | |
"name": "Private" | |
}] | |
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json, re, string, sys | |
input_file_name = sys.argv[1] | |
output_file_name = sys.argv[2] | |
def append_data(obj, ref, val): | |
obj[ref] = val | |
def add_meta(arr, uid_ref, node_type): | |
for i in range(len(arr)): | |
blank_node = arr[i][uid_ref].lower() | |
append_data(arr[i], "uid", "_:{}".format(re.sub(r"[\n\t\s]*", "", blank_node))) | |
append_data(arr[i], "dgraph.type", node_type) | |
if node_type == "User": | |
add_meta(arr[i]["cityn"], "id", "City") | |
add_meta(arr[i]["edun"], "id", "College") | |
add_meta(arr[i]["workn"], "id", "Company") | |
with open(input_file_name, "r") as in_json_file: | |
data = json.load(in_json_file) | |
with open(output_file_name, "w") as out_json_file: | |
add_meta(data, "id", "User") | |
print(data) | |
json.dump(data, out_json_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment