Skip to content

core.update_json

Classes

CheckAcronymAbbrAndFullDict

CheckAcronymAbbrAndFullDict(
    names_abbr="names_abbr", names_full="names_full"
)
Source code in pyformatjson/core/update_json.py
def __init__(self, names_abbr="names_abbr", names_full="names_full"):
    self.names_abbr = names_abbr
    self.names_full = names_full

Functions

update_json_file

update_json_file(full_json_cj, conferences_or_journals)

Update and format JSON file containing conference/journal data.

This function loads JSON data, processes and formats text fields by splitting long text into appropriate lengths, checks for duplicate abbreviations, and saves the updated data back to the file.

Parameters:

Name Type Description Default
full_json_cj str

Full path to the conferences/journals JSON file

required
conferences_or_journals str

Type of publication ('conferences' or 'journals').

required

Returns:

Type Description
dict[str, Any]

dict[str, Any]: Processed JSON data dictionary.

Source code in pyformatjson/core/update_json.py
def update_json_file(full_json_cj: str, conferences_or_journals: str) -> dict[str, Any]:
    """Update and format JSON file containing conference/journal data.

    This function loads JSON data, processes and formats text fields by splitting
    long text into appropriate lengths, checks for duplicate abbreviations, and
    saves the updated data back to the file.

    Args:
        full_json_cj (str): Full path to the conferences/journals JSON file
        conferences_or_journals (str): Type of publication ('conferences' or 'journals').

    Returns:
        dict[str, Any]: Processed JSON data dictionary.
    """
    # Load Json Data
    json_dict = load_json_data(os.path.dirname(full_json_cj), os.path.basename(full_json_cj))

    # Process and format text fields in JSON data.
    for pub in json_dict:
        for flag in ["txt_abouts", "txt_remarks"]:
            data_list = [p for p in json_dict[pub].get(flag, []) if p.strip()]
            temps = []
            for line in split_data_list(r"(\n+)", ["".join(data_list)], "next"):
                temps.extend(split_text_by_length(line, 105))
            if temps:
                json_dict[pub].update({flag: temps})

        for abbr in json_dict[pub][conferences_or_journals]:
            for flag in ["txt_abouts", "txt_remarks"]:
                data_list = [i for i in json_dict[pub][conferences_or_journals][abbr].get(flag, []) if i.strip()]
                temps = []
                for line in split_data_list(r"(\n+)", ["".join(data_list)], "next"):
                    temps.extend(split_text_by_length(line, 97))
                if temps:
                    json_dict[pub][conferences_or_journals][abbr].update({flag: temps})

    # Generate standard form
    abbr_dict = generate_standard_form(json_dict, conferences_or_journals)

    _, flag = CheckAcronymAbbrAndFullDict().run(abbr_dict)

    # Save updated JSON
    if flag and json_dict:
        json_dict = add_multiple_add(json_dict, conferences_or_journals)

        with open(full_json_cj, "w", encoding="utf-8", newline="\n") as f:
            f.write(json.dumps(json_dict, indent=4, sort_keys=True, ensure_ascii=True))

    return json_dict