Skip to content

Json file

JsonFileConf

Bases: BaseModel

JSON config model.

Parameters:

Name Type Description Default
filename str

Term to use for the JSON file name.

'$id'
pop bool

If the field used for the id should be removed from the data.

False
dirpath str

Root directory for JSON files.

required
Source code in stac_generator/plugins/outputs/json_file.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class JsonFileConf(BaseModel):
    """JSON config model."""

    filename: str = Field(
        default="$id",
        description="Term to use for the JSON file name.",
    )
    pop: bool = Field(
        default=False,
        description="If the field used for the id should be removed from the data.",
    )
    dirpath: str = Field(
        description="Root directory for JSON files.",
    )

JsonFileOutput

Bases: Output

Output to a JSON file.

Plugin name: json_out

Example Configuration

.. code-block:: yaml

- name: json_out
  conf:
    dirpath: location_to_destination_file
    filename_term: item_id
Source code in stac_generator/plugins/outputs/json_file.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class JsonFileOutput(Output):
    """
    Output to a JSON file.

    **Plugin name:** ``json_out``

    Example Configuration:
        .. code-block:: yaml

            - name: json_out
              conf:
                dirpath: location_to_destination_file
                filename_term: item_id
    """

    config_class = JsonFileConf

    def export(self, data: dict, **kwargs) -> None:

        terms = re.findall("{(.*?)}", self.conf.filename)

        if self.conf.pop:
            format_terms = {term: data.pop(term, "") for term in terms}
        else:
            format_terms = {term: data.get(term, "") for term in terms}

        filename = self.conf.filename.format(**format_terms)

        filepath = os.path.join(self.conf.dirpath, filename)

        with open(filepath, "w+", encoding="utf-8") as file:
            json.dump(data, file, indent=4)