filesoft.Discuss a project
← Practical FileMaker guides

DATA & CALCULATIONS

Create JSON from FileMaker fields.

Build a JSON object with JSONSetElement, preserve text IDs, inspect the result and read values back in FileMaker.

Filesoft ·

A script often needs to pass several values together: a contact ID, a name and a count. A JSON object gives each value a name, so the receiving script does not have to guess what the third line means.

In this example, you will create one contact object, inspect it, and read its ID back. Start with fictional values, then substitute fields from your own working copy.

1. Decide what each value means.

Use text for identifiers such as 00042, even when they contain digits. Use a number for a count you intend to calculate with. Keep the JSON keys stable: another script may depend on contactId even if you later rename a field label.

  • contactId: text, with leading zeros preserved.
  • name: text, which may contain quotes or accented characters.
  • openTasks: a number.

2. Build a small object.

Evaluate this calculation in FileMaker’s Data Viewer, or use it as the value of a Set Variable step in a practice script. JSON functions used here are available from FileMaker Pro 16. FileMaker Pro is required and sold separately.

FileMaker calculation

Let ( [
    contactId = "00042" ;
    contactName = "Maya Chen" ;
    openTasks = 3
] ;
    JSONSetElement ( "{}" ;
        [ "contactId" ; contactId ; JSONString ] ;
        [ "name" ; contactName ; JSONString ] ;
        [ "openTasks" ; openTasks ; JSONNumber ]
    )
)

JSONSetElement handles the JSON quoting. Avoid assembling the object by joining braces, quotes and field values yourself. Claris documents the function and its type constants.

3. Inspect the result.

Copy the result—not the FileMaker calculation—into the JSON formatter. Format it and check that it represents these values. The order of object keys and the spacing may differ.

Expected JSON values

{
  "contactId": "00042",
  "name": "Maya Chen",
  "openTasks": 3
}

The ID is quoted; the count is not. Formatting makes this easier to see, but valid JSON can still contain the wrong values or types. Check the receiving script’s expectations too.

4. Replace the sample values with fields.

In your file, assign the three local calculation variables from your own fields. For example, use Contacts::ContactID for contactId and Contacts::FullName for contactName. These are example names, not fields supplied by this article.

Run from a layout based on the intended contact table occurrence and confirm the current record. A correct formula in the wrong context can package the wrong contact. Decide explicitly how your receiving script should treat a blank name or missing count; an empty string and JSON null are different values.

5. Read one value back.

If your JSON is stored in $payload, this calculation retrieves its contact ID:

FileMaker calculation

JSONGetElement ( $payload ; "contactId" )

For the sample above, expect the text 00042. The key is case-sensitive: use contactId, not ContactID. See Claris’s JSONGetElement reference.

Check before using real records.

  • Try a name containing quotation marks, such as Maya "MJ" Chen. The extracted name should match what you entered.
  • Try an ID beginning with zeros. If they disappear, check the source field and keep the JSON type as text.
  • If FileMaker reports an error, inspect the formula and field context before copying the result to the formatter.

The browser formatter checks JSON syntax; it cannot check your fields or run your FileMaker calculation. Use fictional data while learning.

PUT IT INTO PRACTICE

Try it with Filesoft.

Open the JSON formatter

Want a guided learning path? Explore the free FileMaker courses.

Continue reading

Reference: JSONSetElement · JSONGetElement. Examples are learning aids; check them in your own FileMaker working copy.