-1

How can I write into existing JSON-file the data from backend-Formular in Typo3 directly? And how can I later delete some data from JSON, when I delete them from the backend in Typo3? All JSON enterings have a time stamp.

Thank you in advance!

1 Answers1

0

as JSON, is not a linear file Format but rather a Serialized object. its not possible to append data to it.

what you have to to is:

1) read the json file 2) use json_decode() to get an stdObject or array 3) manipulate that data Object/array the way you want. 4) use json_encode() to convert it back to a string.. 5) write it back to a file.

// Load The Data
$pathToYourFile = 'paht/somefile.json';
$dataString = file_get_contents($pathToYourFile);
$jsonDataArray = json_decode($dataString, true); // fetch Data as ArrayM
///Maniuplate the data like you want

$jsonDataArray['someValue'] = 'updated Value';

// write it back

$dataString = json_encode($jsonDataArray);
file_put_contents($dataString);
Wolffc
  • 1,176
  • 6
  • 9
  • Thanks a lot for your answer. I would try this out. Another question I have. Is it possible to delete some data from existing JSON-file, when they would be deleted from the backend-form in Typo3? E.g. is it possible to find and compare needed data via timestamp to delete? – MarinaS Feb 26 '20 at 00:53