Skip to content

g.list: Add JSON support #5921

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

NishantBansal2003
Copy link
Contributor

Fixes: #5848

This PR adds JSON support to the g.list module. The JSON output looks like:

[
    {
        "mapset": "test_1",
        "name": "raster_test_1",
        "type": "raster",
        "fullname": "raster_test_1@test_1"
    },
    {
        "mapset": "test_1",
        "name": "vector_test_1",
        "type": "vector",
        "fullname": "vector_test_1@test_1"
    }
]

This PR includes the following changes:

  1. Adds a format option with plain, shell, and json modes for output formatting.
  2. The flags -m and -t apply only when format=shell.
  3. The flag -p is deprecated; use format=plain instead. The flag -f is also deprecated.
  4. Adds tests covering each of the new formats.
  5. Adds a Python example to the documentation for parsing JSON output.

Signed-off-by: Nishant Bansal <[email protected]>
@github-actions github-actions bot added Python Related code is in Python C Related code is in C module docs markdown Related to markdown, markdown files general tests Related to Test Suite CMake labels Jun 20, 2025
@cwhite911 cwhite911 moved this to In Progress in GRASS JSON Outputs Jun 20, 2025
Copy link
Contributor

@petrasovaa petrasovaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just couple minor points.

Comment on lines 127 to 130
G_json_object_set_string(map_object, "mapset", el[i].mapset);
G_json_object_set_string(map_object, "name", el[i].name);
G_json_object_set_string(map_object, "type", el[i].type);
G_json_object_set_string(map_object, "fullname", fullname);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's switch the order to have name, mapset, type, fullname:

Suggested change
G_json_object_set_string(map_object, "mapset", el[i].mapset);
G_json_object_set_string(map_object, "name", el[i].name);
G_json_object_set_string(map_object, "type", el[i].type);
G_json_object_set_string(map_object, "fullname", fullname);
G_json_object_set_string(map_object, "mapset", el[i].mapset);
G_json_object_set_string(map_object, "name", el[i].name);
G_json_object_set_string(map_object, "type", el[i].type);
G_json_object_set_string(map_object, "fullname", fullname);

```python
import grass.script as gs

data = gs.parse_command("g.list", type=["rast", "vect"], format="json")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data = gs.parse_command("g.list", type=["rast", "vect"], format="json")
data = gs.parse_command("g.list", type=["raster", "vect"], format="json")

Copy link
Member

@wenzeslaus wenzeslaus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The messaging around -p needs to be little different here because the default format here is "shell" and there is a special flag for plain (while most of the other tools have default "plain" with -g for shell).

@@ -27,7 +33,7 @@ g.list type=vector -m
List all raster and vector maps ordered by mapset:

```sh
g.list type=raster -p
g.list type=raster format=plain
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-p is for interactive use, so typing format=plain does not make much sense. While not ideal, in this case, -p needs to stay as is for now, so here:

Suggested change
g.list type=raster format=plain
g.list type=raster,vector -p

(I'm also fixing the type to align with the description above that.)

@@ -27,7 +33,7 @@ g.list type=vector -m
List all raster and vector maps ordered by mapset:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
List all raster and vector maps ordered by mapset:
List all raster and vector maps grouped by mapset in plain format:

Comment on lines 14 to 16
The flags `-p` and `-f` apply only to plain format output, but they are now
deprecated and will be removed in a future release. Instead of using those, use
`format=plain` to obtain human-readable plain text output.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The flags `-p` and `-f` apply only to plain format output, but they are now
deprecated and will be removed in a future release. Instead of using those, use
`format=plain` to obtain human-readable plain text output.
The flags `-p` and `-f` apply only to plain format output, but they are now
deprecated and will be removed in a future release. Instead of using those, use
`format="plain"` to obtain human-readable, plain text output. The plain format will
become the default in a future release.
To obtain machine readable output use `format="shell"` or `format="json"`.

```python
import grass.script as gs

data = gs.parse_command("g.list", type=["rast", "vect"], format="json")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the shorter versions work, the full versions are the best practice for a Python script.

Suggested change
data = gs.parse_command("g.list", type=["rast", "vect"], format="json")
data = gs.parse_command("g.list", type=["raster", "vector"], format="json")

### Mapset search path

If **mapset** is not specified, then *g.list* searches for data files in
the mapsets that are included in the search path (defined by
*[g.mapsets](g.mapsets.md)*). See `g.mapsets -p`.

```sh
g.list rast -p
g.list rast format=plain
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
g.list rast format=plain
g.list rast -p

@@ -72,7 +93,7 @@ available mapsets also including those that are not listed in the
current search path (see `g.mapsets -l`).

```sh
g.list rast mapset=* -p
g.list rast mapset=* format=plain
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
g.list rast mapset=* format=plain
g.list rast mapset=* -p

Comment on lines 159 to 160
"This flag is deprecated and will be removed in a future release. Use "
"format=plain instead.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This better describes the situation. Let me know if it makes sense:

Suggested change
"This flag is deprecated and will be removed in a future release. Use "
"format=plain instead.");
"This flag is deprecated and will be removed in a future release. The "
"plain format will become the default. Use format=\"plain\" to set it "
"explicitly in a script.");

flag.full->description = _("Verbose listing (also list map titles)");
flag.full->label = _("Verbose listing (also list map titles) [deprecated]");
flag.full->description =
_("This flag is deprecated and will be removed in a future release.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is little nicer to tell user what to do instead:

Suggested change
_("This flag is deprecated and will be removed in a future release.");
_("This flag is deprecated and will be removed in a future release. "
" Use the type-specific tool to obtain metadata, such as r.info "
"and v.info.");

Comment on lines 181 to 183
G_verbose_message(
_("Flag 'p' is deprecated and will be removed in a future "
"release. Please use format=plain instead."));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
G_verbose_message(
_("Flag 'p' is deprecated and will be removed in a future "
"release. Please use format=plain instead."));
// Do not switch this to a warning until v9. The depreciation is
// documented, but warning would be too distracting given the
// likely interactive use of this flag. In v9.0, either remove it
// or make this into a warning and remove it in v9.1.
G_verbose_message(
_("Flag 'p' is deprecated and will be removed in a future "
"release where plain will be the default format. Use "
"format=\"plain\" to set plain format for scripting."));

Signed-off-by: Nishant Bansal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C Related code is in C CMake docs general markdown Related to markdown, markdown files module Python Related code is in Python tests Related to Test Suite
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

[Feat] JSON output for g.list
3 participants