Ask Reuben

What is in a name?

I am parsing a JSON string but the name of the JSON key is not a legal 4gl identifier ? 

I am parsing an XML Document but the name of a node or attribute is not a legal 4gl identifier ?

I am interacting with a Web Service but a parameter name is not a legal 4gl identifier?

The Genero syntax for an identifier includes the following properties …

  • only ASCII letters, numbers, and the underscore (_) character are permitted
  • blanks, hyphens, and other non-alpha numeric characters are not permitted
  • the initial character must be a letter or underscore

So first_name is a valid Genero identifier whilst first-name, first.name, “first name”, 1_first_name are not valid Genero identifiers.

Standards such as OpenAPI, JSON, XML etc allow a wider range of identifiers and this can initially cause difficulties if you are attempting to pass data between Genero data structures and other data structures.  For instance a JSON string might be {“first-name”: “Reuben “, “last-name”:”Barclay”} but if trying to parse that into a 4gl record variable, the 4gl record members cannot be named first-name, last-name.  This situation will typically be encountered where the non Genero  equivalent has a hyphen (-), dot (.), or space ( ) in the name, or perhaps begins with a digit, or is case sensitive.

The solution when encountering this type of issue is to look for attributes such as

These can be used to give an alternative name for a Genero identifier that matches the actual name as used in the Web Service, XML, or JSON, and will be used when transposing between the two.

For example, try the following small program

IMPORT util
MAIN
DEFINE cust RECORD
    first_name STRING ATTRIBUTES(json_name = "first-name"),
    last_name STRING
END RECORD
    LET cust.first_name = "Reuben"
    LET cust.last_name = "Barclay"
    DISPLAY util.JSON.stringify(cust)
END MAIN

… and note with the output …


{"first-name":"Reuben","last_name":"Barclay"}

… how the json_name attribute is used to transpose “first_name” to “first-name”.