Chapter 9: a collection as JSON

JSON is how programs hand data to each other, and EK9 gives every collection a one-character way to produce it: the $$ operator. The result is a JSON value you can read fields from, so the data is never trapped in text. The IDE makes the program's shell; you type the two sections, run them, then ask where $$ comes from and what a JSON value can do. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-json-taste.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.jsontaste

  defines program
    Show()
      stdout <- Stdout()

      // A Dict of prices, in pence.
      prices <- {"apple": 35, "bread": 80}

      // Any collection becomes JSON with the $$ operator.
      asJson <- $$prices
      stdout.println(`prices as JSON: ${asJson}`)

      // A JSON value is not a dead end: read a field back out by name with get.
      apple <- asJson.get("apple")
      stdout.println(`apple field: ${apple}`)

//EOF

Step 1: The program, and a Dict as JSON

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

Show is the program's name - no application to bind, no parameters, nothing injected. Type Show and choose Preview.

The EK9 IDE just before choosing Preview: Show is the program's name - no application to bind, no parameters, nothing injected

The preview shows Show about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Show about to be written

Select the stub, from ready <- true down to its //AI line, and type the first section: a Stdout, a Dict of prices, and $$ turning it into JSON, printed. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type the first section

Step 2: Reading a field back

Select the last println and type the closing section: get reads the apple field back out of the JSON value.

The EK9 IDE once this is done: Select the last println and type the closing section

Step 3: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

The project's only program is already chosen. Choose Run.

The prices as JSON, {"apple": 35, "bread": 80}, then the apple field read back out: 35. Choose Close.

The EK9 IDE just before choosing Close: The prices as JSON, {"apple"

Step 4: Ask: /q 189

Type /q 189 into the input bar above the editor and choose -> to send it - a direct lookup, by the question's own number, into the Answers tab.

The EK9 IDE once this is done: Type /q 189 into the input bar above the editor and choose -> to send it - a direct lookup, by the question's own number, into the Answers tab

The Answers tab shows Q189: the $$ operator converts values to JSON - every built-in type supports it, and a record with default operator $$ serializes all its fields.

The EK9 IDE once this is done: The Answers tab shows Q189

Step 5: Read Effective API: JSON

Select asJson, on the line that declares it, then choose Analyse ▸ Effective API - the same pane chapter 9's Basket read for a List and a Dict.

The EK9 IDE once this is done: Select asJson, on the line that declares it, then choose Analyse ▸ Effective API - the same pane chapter 9's Basket read for a List and a Dict

The pane draws the JSON type's own surface: the get the program called to read a field by name is among its Methods, beside the rest of what a JSON value can do.

The result

JsonTaste.ek9 now reads exactly as chapter 9 of Getting Started with EK9 shows it: a Dict of prices turned into JSON with $$, and one field read back out by name with get. The IDE's tool made the program; the two sections were typed and run. /q 189 and Analyse ▸ Effective API then showed where $$ and get come from.

Where this lesson comes from