grass icon indicating copy to clipboard operation
grass copied to clipboard

[Feat] Add JSON output for columns in v.info

Open wenzeslaus opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe.

The use of c flag for v.info should produce JSON with format=json:

grass --tmp-mapset ~/grassdata/nc_spm_08_grass7/ --exec v.info roadsmajor format=json -c

Describe the solution you'd like

JSON output which contains column names and metadata for these columns. Type is already in the current output, but there is more what could be included: other types of types (e.g., C versus SQL: db_sqltype_to_Ctype) or some info on whether the column represents a number or whether it needs to be quoted in SQL (e.g., #1110).

Describe alternatives you've considered

The output could be a dictionary (mapping) or a list. In any case, the format should be extensible so more info can be included in the future. If a single column is a dictionary, then there is no problem with extending that. The question is if the list of columns should be in a dictionary.

Taking route of maximum flexibility:

{
  "columns": [
    {
      "name": "cat",
      "sql_type": "INTEGER",
    },
    {
      "name": "MAJORRDS_",
      "sql_type": "DOUBLE PRECISION",
    }
  ]
}

Flexible only for the individual columns (simpler, but (perhaps?) less common list as a first level):

[
  {
    "name": "cat",
    "sql_type": "INTEGER",
  },
  {
    "name": "MAJORRDS_",
    "sql_type": "DOUBLE PRECISION",
  }
]

Shortest possible:

[
  {
    "name": "cat",
    "type": "INTEGER",
  },
  {
    "name": "MAJORRDS_",
    "type": "DOUBLE PRECISION",
  }
]

I don't think we should do this:

{
  "names": ["cat", "MAJORRDS_"],
  "types": ["INTEGER", "DOUBLE PRECISION"]
}

Additional context

The output should be similar, compatible and/or in sync with v.db.select:

$ grass --tmp-mapset ~/grassdata/nc_spm_08_grass7/ --exec v.db.select roadsmajor format=json | jq .info
{
  "columns": [
    {
      "name": "cat",
      "sql_type": "INTEGER",
      "is_number": true
    },
    {
      "name": "MAJORRDS_",
      "sql_type": "DOUBLE PRECISION",
      "is_number": true
    },
    {
      "name": "ROAD_NAME",
      "sql_type": "CHARACTER",
      "is_number": false
    },
    {
      "name": "MULTILANE",
      "sql_type": "CHARACTER",
      "is_number": false
    },
    {
      "name": "PROPYEAR",
      "sql_type": "INTEGER",
      "is_number": true
    },
    {
      "name": "OBJECTID",
      "sql_type": "INTEGER",
      "is_number": true
    },
    {
      "name": "SHAPE_LEN",
      "sql_type": "DOUBLE PRECISION",
      "is_number": true
    }
  ]
}

Currently, the following is produced with the c flag:

Displaying column types/names for database connection of layer <1>:
INTEGER|cat
DOUBLE PRECISION|MAJORRDS_
CHARACTER|ROAD_NAME
CHARACTER|MULTILANE
INTEGER|PROPYEAR
INTEGER|OBJECTID
DOUBLE PRECISION|SHAPE_LEN

(The Displaying column ... text goes to stderr and should not be produced for JSON.)

We do have tools which give lists, e.g., g.gisenv get="GISDBASE,LOCATION_NAME,MAPSET" sep="comma", but once the output is more complex, list on the first level seems too limiting - I included also info about the columns to v.db.select so that the full information about the type is preserved and can be used to, e.g., create a derived table.

wenzeslaus avatar Aug 23 '24 17:08 wenzeslaus

@wenzeslaus Please assign this issue to me. I would like to give my attempt to it.

Adi-456 avatar Sep 16 '24 14:09 Adi-456

Hey @wenzeslaus, should I build this JSON format using the Parson library or by simply using fprintf? I checked v.db.select, and it turns out that they are not using the Parson library for JSON output; instead, they are using fprintf to format the JSON.

NishantBansal2003 avatar Oct 24 '24 18:10 NishantBansal2003

@NishantBansal2003 Please use Parson, the reason v.db.select is not using it is that it the JSON output was implemented before Parson was included. However, v.db.select should be a good starting point for the output format and content.

wenzeslaus avatar Oct 24 '24 20:10 wenzeslaus