-- import: fastn/processors as pr
-- string foo:
$processor$: pr.get-data
file: foo.json
This will read the key foo
from foo.json
and store it in the variable named
foo
.
Key
By default, the name of the variable or list where the data is being store is
used as the key. You can overwrite the key using key
attribute:
-- import: fastn/processors as pr
-- string foo:
$processor$: pr.get-data
key: some-other-key-instead-of-foo
file: foo.json
Default Value
If the data is not found, we use the body as default value if body is available.
-- import: fastn/processors as pr
-- string foo:
$processor$: pr.get-data
"hello world"
The body must be valid json, compatible with the data type on which we are
using the get-data
processor.
Default Value in Caption For Primitive Types
For
primitive types like
integer
,
boolean
,
string
etc, the default value can also be provided in the caption.
E.g.
-- import: fastn/processors as pr
-- string foo: hello world
$processor$: pr.get-data
Providing both body
and caption
when using get-data
is an error.
Tutorial
We will be reading the data from JSON file and injecting the value to the caller
of the processor (caller could be variable or component).
Creating index.ftd
We need to make two files i.e. one file should be index.ftd
and another
file should be foo.json
-- import: fastn/processors as pr
-- record person:
caption name:
integer age:
string gender:
-- person arpita:
$processor$: pr.get-data
file: foo.json
-- ftd.text: $foo.name
-- ftd.text: $foo.age
-- ftd.text: $foo.gender
NOTE: file
must point to a valid json
file with extension .json
.
{
"name": "arpita",
"age": 15,
"gender": "female"
}
Running
Run fastn serve
and view 127.0.0.1:8000
(use whatever port reported by
fastn serve
), and you should see something like this: