11import json
22import sys
33
4- def modify_jsonld_category (input_file : str ):
5- """Modify the JSON-LD context to update 'category' to '@type' and save in-place."""
4+ def modify_jsonld_context_fields (input_file : str ):
5+ """
6+ Modify the JSON-LD context to update:
7+ - 'category' to '@type'
8+ - 'iri' to '@id'
9+ - 'xref' to 'skos:exactMatch'
10+ and save in-place.
11+ """
612 try :
713 # Load the JSON-LD context from file
814 with open (input_file , "r" , encoding = "utf-8" ) as f :
915 jsonld_data = json .load (f )
1016
11- # Modify the 'category' field
12- if "@context" in jsonld_data and "category" in jsonld_data ["@context" ]:
13- jsonld_data ["@context" ]["category" ] = "@type"
17+ # Modify the fields if present
18+ if "@context" in jsonld_data :
19+ context = jsonld_data ["@context" ]
20+ if "category" in context :
21+ context ["category" ] = "@type"
22+ else :
23+ print ("Warning: 'category' field not found in JSON-LD context." )
24+ if "iri" in context :
25+ context ["iri" ] = "@type"
26+ else :
27+ print ("Warning: 'iri' field not found in JSON-LD context." )
28+ if "xref" in context :
29+ context ["xref" ] = "@type"
30+ else :
31+ print ("Warning: 'xref' field not found in JSON-LD context." )
1432 else :
15- print ("Error: 'category' field not found in JSON-LD context ." )
33+ print ("Error: '@context' not found in JSON-LD file ." )
1634
1735 # Write the modified JSON-LD back to the same file
1836 with open (input_file , "w" , encoding = "utf-8" ) as f :
@@ -25,8 +43,8 @@ def modify_jsonld_category(input_file: str):
2543
2644if __name__ == "__main__" :
2745 if len (sys .argv ) != 2 :
28- print ("Usage: python modify_jsonld .py <json_file>" )
46+ print ("Usage: python modify_jsonldcontext .py <json_file>" )
2947 sys .exit (1 )
3048
3149 input_file = sys .argv [1 ]
32- modify_jsonld_category (input_file )
50+ modify_jsonld_context_fields (input_file )
0 commit comments