In your page-fragment's pre-page script, you can create a local array that is sparse and multi-dimensioned, eg:

s person("Name")="Rob"
s person("DateOfBirth")="13/06/1955"
s person("Address","PostCode")="SW1 3QA"
s person("Address","Line1")="1 The Street"
s person("Address","2","hello")="world"

You can then merge this into an EWD Session Object, eg:

 d mergeArrayToSessionObject^%zewdAPI(.person,"person",sessid)

Alternatively you could achieve the same result by creating each property explicitly, eg:

 d setSessionValue^%zewdAPI("person.Name","Rob",sessid)
 d setSessionValue^%zewdAPI("person.DateOfBirth","13/06/55",sessid)
 d setSessionValue^%zewdAPI("person.Address.PostCode","SW1 3QA",sessid)
 d setSessionValue^%zewdAPI("person.Address.Line1","1 The Street",sessid)
 d setSessionValue^%zewdAPI("person.Address.2.hello","world",sessid)

Now convert this Session Object to a JSON string and save it as a session variable:

  d setJSONValue^%zewdAPI("json","person",sessid)

In this example we now have a session variable named json that contains the JSON string defining the person object.

Note: calling the setJSONValue() method automatically sets read access for the Session Array (person in this example) so that it can be fetched into the browser using JSON.

You can now load this JSON string into your page fragment by adding the ewd.getJSON() pseudo-function into a Javascript tag:

  <script language="javascript">
    ewd.getJSON(#json) ;

This is simply a shortcut that is synonymous with:

  <script language="javascript">
    eval('<?= #json ?>') ;

You can use either syntax depending on your preference.

When the fragment loads, the person object will be instantiated in Javascript and will be available from that point forwards. For example:

  alert('postCode = ' + person.Address.PostCode) ;

Note the EWD Session Object property that has a numeric part:


This should be referenced in Javascript as:

  alert('hello ' + person.Address[2].hello) ;

This highlights the fact that both EWD and Javascript objects can also be viewed as arrays.

Note: you can view a Javascript Object as a JSON String using the toJsonString() method:

  alert(toJsonString(person)) ;