Support us by giving us a
star on Github 🚀

Reading JSON Using `fastn`

`get-data` processor is used to get data from a JSON files in the package.
-- import: fastn/processors as pr

-- string foo:
$processor$: pr.get-data
file: foo.json
Lang:
ftd
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
Lang:
ftd

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"
Lang:
ftd
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](/built-in-types#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
Lang:
ftd
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`
`index.ftd`
-- 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
Lang:
ftd
NOTE: `file` must point to a valid `json` file with extension `.json`.

Creating `foo.json`

{
"name": "arpita",
"age": 15,
"gender": "female"
}
Lang:
json

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:
arpita
15
female
Lang:
txt

Support `fastn`!

Enjoying `fastn`? Please consider giving us a star ⭐️ on [GitHub](https://github.com/fastn-stack/fastn) to show your support!
[⭐️](https://github.com/fastn-stack/fastn)

Getting Help

Have a question or need help? Visit our [GitHub Q&A discussion](https://github.com/fastn-stack/fastn/discussions/categories/q-a) to get answers and subscribe to it to stay tuned. Join our [Discord](https://discord.gg/a7eBUeutWD) channel and share your thoughts, suggestion, question etc. Connect with our [community](/community/)!
[💻️](/community/)

Found an issue?

If you find some issue, please visit our [GitHub issues](https://github.com/fastn-stack/fastn/issues) to tell us about it.

Quick links:

- [Install `fastn`](install/) - [Create `fastn` package](create-fastn-package/) - [Expander Crash Course](expander/) - [Syntax Highlighting in Sublime Text](/sublime/)

Join us

We welcome you to join our [Discord](https://discord.gg/a7eBUeutWD) community today. We are trying to create the language for human beings and we do not believe it would be possible without your support. We would love to hear from you.
Copyright © 2023 - fastn.com