How to access file? Newbie is stuck

Good evening,

I have a very simple thing I want to accomplish but I am stuck. The task: in my site/config directory, I have a file called “something” which contains a string representing data in JSON format.

I now want to read the file and (assuming I would automatically get an object) return one specific value of the object called “otherthing”.

But I don’t get to this point - I fail on even reading the file. The new File() docs is really not helpful. It says I should use $props[‘url’] and when trying to set the URL (wrongly?) the debugger complains about invalid arguments, stating “The filename and parent are required”. I tried to add a “root” value to the array but no luck either.

$textFile = new File(['url' => $kirby->url('site/config/something')], );
$file = $textFile->read();

return $file->otherthing;

What am I a doing wrong here?

Thanks for your help,
Andreas

The File class is not really needed or even usefule here, and if you have.a json file in the /site/config folder, it is not accessible via a URL anyway.

Simply use

$json  = F::read(kirby()->root('config') . '/something.json';
$array = json_decode($json, true); // true to get an array, remove if you prefer to work with an object.
1 Like

Wow, thanks a lot for your quick response - it works perfectly!