Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new script 'modify_jsonldcontext.py' #146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions utils/modify_jsonldcontext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
import sys

def modify_jsonld_category(input_file: str):
"""Modify the JSON-LD context to update 'category' to '@type' and save in-place."""
try:
# Load the JSON-LD context from file
with open(input_file, "r", encoding="utf-8") as f:
jsonld_data = json.load(f)

# Modify the 'category' field
if "@context" in jsonld_data and "category" in jsonld_data["@context"]:
jsonld_data["@context"]["category"] = "@type"
else:
print("Error: 'category' field not found in JSON-LD context.")
sys.exit(1)

# Write the modified JSON-LD back to the same file
with open(input_file, "w", encoding="utf-8") as f:
json.dump(jsonld_data, f, indent=3)

print(f"Updated JSON-LD context saved to {input_file}")
except Exception as e:
print(f"Error processing JSON-LD: {e}")
sys.exit(1)

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python modify_jsonld.py <json_file>")
sys.exit(1)

input_file = sys.argv[1]
modify_jsonld_category(input_file)