JSON in config file

50 views
Skip to first unread message

Chris Maxwell

unread,
Jan 21, 2019, 1:12:50 PM1/21/19
to Fat-Free Framework
Is it possible somehow to include JSON in a F3 config file? For example:

somearray={"title": "Apple", "color": "green", "size": "medium"}
somearray
={"title": "Orange", "color": "orange", "size": "medium"}
somearray={"title": "Lemon", "color": "lemon", "size": "medium"}

When this is present F3 seems to break it up based on commas as it's presumably trying to auto-detect an array/list (as per default behaviour for comma separated lists in config files).

We have managed to work around this by making a pretty clunky function to run through the array to re-form the JSON, then parse it into an array.

However, I'm wondering if we are over-complicating this and if there may be a better solution.

Does anyone have any ideas?

Cheers,
Chris

ved

unread,
Jan 21, 2019, 2:44:13 PM1/21/19
to Fat-Free Framework
Hi, there are some ways to achieve that.
If you really need json on the config, placing the json inside single quotes should work without F3 splitting it into an array on the commas.

For example:

config.ini:
somearray = '{"title": "Lemon", "color": "lemon", "size": "medium"}'

Then in your php files use:
$json = json_decode($f3->get('somearray'), true);

I think the above should work as you intended.

You can also use regular variables and arrays on the config.ini and then use json_encode to make it json.
I think this is a more legible way than placing json directly on the config, but either way should work.

For example:

config.ini:
[customsection]
var1
=somevalue
var2
=somevalue2
var3
=this,is,an,array

Then in your php files use:
$json = json_encode($f3->get('customsection'));

Which should transform it into something like:

{"var1":"somevalue","var2","somevalue2","var3": ["this","is","an","array"]}

Hope it helps, good luck.

ikkez

unread,
Jan 21, 2019, 6:39:31 PM1/21/19
to Fat-Free Framework
you can also just use custom regions in your config.ini to simplify setting up this array:

[somearray.0]
title
= Apple
color
= green
size
= medium

[somearray.1]
title
= Orange
color
= orange
size
= medium

[somearray.2]
title
= Lemon
color
= lemon
size
= medium

Chris Maxwell

unread,
Jan 22, 2019, 10:56:00 AM1/22/19
to Fat-Free Framework
Thanks for your replies.

The single quote solution would be best for simplicity, but unfortunately doesn't seem to work - the JSON inside single quotes still gets split based on the commas in the string. Any ideas for how to prevent it splitting into an array on commas?

The custom regions suggestion is nice but this would make the config file much longer (in terms of number of lines). However, it's currently the only feasible workaround until we can get JSON working.

ikkez

unread,
Jan 22, 2019, 11:03:27 AM1/22/19
to Fat-Free Framework
Have you tried double qoutes? Nevertheless, it's a config file for configuration values.. json is probably not a good format to use for manual editing,.. at least it's a data exchange format. So I would suggest to put the data in a .json file and read & decode it elsewhere.. the config.ini could then just hold the filename for your json file.
Reply all
Reply to author
Forward
0 new messages