Generating URLs from JSON data

You can use JSON data to create a standard URL query. The result is a root URL, followed by a question mark, followed by a list of URL argument assignments from JSON data, separated by ampersands. The syntax is similar to:

http://example.com/page?arg1=val1&arg2=val2

Create a URL of this type using the static Gosu enhancement method makeUrl on the java.net.URL class. Pass a base URL as the first argument, and the JSON data as the second argument. The JSON data must be a bindings object like javax.script.SimpleBindings or anything that implements interface javax.script.Bindings. A Gosu expando object, such as the Gosu class Expando, satisfies the Bindings requirement.

The following example Gosu code creates JSON-like data in two ways and generates a URL that uses the JSON data for query arguments with String and number values. Numbers are coerced to a String to serialize them into text.

uses gw.lang.reflect.Expando
uses java.net.URL

var rootUrl = "http://example.com/en"

// ** Using Dynamic type and intialization syntax
var d: dynamic.Dynamic = new() {
  :Name = "Andy Applegate",
  :Age = 39
}
var url = URL.makeUrl(rootUrl, d)
print("Using dynamic bindings object = " + url)

// ** Using explicit Expando instantiation
var e : dynamic.Dynamic = new Expando()
e.Name = "John Smith"
e.Age = 44
url = URL.makeUrl(rootUrl, e)
print("Using dynamic expando object = " + url)

This code prints:

Using dynamic bindings object = http://example.com/en?Name=Andy+Applegate&Age=39
Using dynamic expando object = http://example.com/en?Name=John+Smith&Age=44

Note that space characters became + characters. This transformation is part of the process called URL encoding. Be aware that some special characters may become multiple characters as part of URL encoding.

The previous example used Bindings values that were String objects or numbers. The makeUrl method also supports values of other complex types:

  • If the value is a javax.script.Bindings object, a Gosu dynamic expando object, or a list, Gosu transforms the object to JSON String data.
  • Any other value is coerced to a String.

Finally, in all cases, the values are URL encoded before becoming part of the final URL.

The following example creates a new URL from a Bindings object with values that include one Bindings object and one object of list type List<String>:

uses javax.script.Bindings
uses java.net.URL

var rootUrl = "http://example.com/en"

var d: dynamic.Dynamic = new() {
  :Who = new() { :FirstName="Andy", :LastName="Applegate" },
  :Likes = {"Hiking", "Football"}
}

print("JSON of :Who = " + d.Who.toJson())
print("JSON of :Likes = " + Bindings.listToJson(d.Likes))

var url = URL.makeUrl(rootUrl, d)
print ("URL encoding = " + url)

The URL output for this example is harder to read because of URL encoding of special characters such as braces, comma characters, and new-line characters. Also, multiple + characters represent space characters for each line of indented JSON.

This example prints the following, which is shown formatted with added new lines in the URL for clarity:

JSON of :Who = {
  "FirstName": "Andy",
  "LastName": "Applegate"
}
JSON of :Likes = [
    "Hiking",
    "Football"
  ]
URL encoding = http://example.com/en
?Who=%7B%0A++%22FirstName%22%3A+%22Andy%22%2C%0A++%22LastName%22%3A+%22Applegate%22%0A%7D
&Likes=%5B%0A++++%22Hiking%22%2C%0A++++%22Football%22%0A++%5D

See also