Since I have used it in a number of presentations, and also distributed it to attendees at my recent OAuth2 and REST courses at EDUC 2016, I thought I should make this little program publicly available.

Note: it comes with exactly no warranty - use at your own risk! That said, I have used it to create all of the structs and handler code for the MS Office 365 API, so it has received substantial testing.

What it does is basically take JSON data and generates DataFlex structs to hold that data (otherwise a very tedious and error-prone process), along with code to convert from JSON to struct variables and visa-versa.

You paste sample JSON into the big edit window, give the base struct a name, give inner structs a name prefix (defaults to the outer struct name - stick with that unless you are sure you know better), tell it where you want to put the generated packages and where that is in relation to your AppSrc directory. You can also set the indentation level to match your preference.

The way I use it is to add it to my Studio "Tools" menu (in the Studio, "Tools" -> "Configure Tools Menu..."), adding a "<workspace>" parameter, which allows the program to default to putting the generated packages somewhere sensible. That way you need only paste in the JSON, give the outer struct a sensible name then click generate and the packages will be placed in your workspace's AppSrc\ApiStructs folder (which will be created if it does not already exist).

When it runs it will give a summary of what it is doing in an output window, along with any warnings. The most common warning is that it has encountered an empty array in the sample JSON, so does not know what "type" of data the array should hold - it will default that type to String, but if that is wrong, you should correct the JSON and hit "Generate" again, overwriting the previously generated packages (warning: it does that silently, so name things carefully!).

There is a limit to the amount of JSON the text window can hold, so you might need to trim large JSON. To that end, be aware that it only examines the first element in each array to determine the type of the array elements, so having an array with many elements is one thing you can cut out (leave in the best populated example).

Once you have generated your struct packages, you need only "Use" the outer one in your code - it will "Use" any inner ones (recursively if required), then create a variable of the type name you gave it. Then there are two procedures you can call to work with the data, passing the two arguments by reference:

Code:
Use ApiStructs\TheNameYouGave.pkg

...

    TheNameYouGave tData
    String sJson

    // To populate a struct variable from JSON:
    Send JsonStringToStruct of oStructHandler_TheNameYouGave (&sJson) (&tData)

    // To generate JSON from the contents of a struct variable:
    Send StructToJsonString of oStructHandler_TheNameYouGave (&tData) (&sJson)
As well as generating handler code, it also generates translations, so that if the JSON contained member names which could not be used as DataFlex struct member names (Microsoft's "@odata.xxxxx" names being one example, but there are many others) it will translate them both going in and out, so on the way in "@odata.id" would become the struct member name "_odata_id", while on the way out "_odata_id" would become "@odata.id" in the generated JSON.

A minor tip: the Studio will do a better job of prompting you with the right member names if you open the packages concerned in it (I tend to just right-click on the type name and select "Go To Definition").

RESTGen.zip

You should unzip the file into its own directory and run RESTGen from there. This version is for DF 18.2 - if you need an 18.1 version, just let me know.

Please report bugs etc. here, so everybody can benefit.

Mike