Json filetype in (Contact)form with flex

I was wondering if it is possible to create a contactform with

process:
    - save:
        filename: feedback.json
        body: "{% include 'forms/data.txt.twig' %}"
        operation: add

(in json / flex format ) so that I can manage (in the backoffice) the feedback with a flex solution. I would be happy if there some example because I am not that good in creating a twig ans so.

Or is it not possible at all?
Peter

You should create a JSON template in your theme. Eg. /user/themes/myTheme/templates/forms/data.json.twig with content:

{{ fields|json_encode|raw }}

(TBH, not sure about content of the template though - would need to test properly)

Then change your save action body to:

        body: "{% include 'forms/data.json.twig' %}"

@Karmalakas thanks for the answer. I took this as a start and finally found a solution.
The twig makes from a contact form a json record. I generate a unique random code as key.

{% macro render_field(form, fields) %}
{% set record = [ ] %}
{% for index, field in fields %}
{% set value = form.value(field.name) %}
{% set veld = field.label ~ ':' %} 
{% set record = record|merge([veld, value]) %}
{% endfor %}
{% set code = random_string(15)|md5 %}
{{ { (code): record}|json_encode|raw }}  
{% endmacro %}
{{ _self.render_field(form, form.fields) }}

However, when I try to read these records as a database by a flex object it fails.
I don’t know the solution for this or is the twig not good? Who does?

Or is there another solution that can be created FI a twig file where a front-end form can be presented as a flex-object?

Not sure what you mean by:

Could you explain in more detail how you want to access this JSON file?

As start I used https://github.com/trilbymedia/grav-plugin-flex-objects. Instead of “Contacts”, referred in this document, I created an own json file with records. I can add, edit, delete records in admin (the back office) and present them with pages in the front office.
What I want is that I can add records with a contact page ( front office) to this json file. With Admin I can edit the records or even delete then when action has taken place.
Hopefully this is more clear?

OK, so you want to save an object when someone fills the form on a page in front-end (front office)? If so, there are some bad news - flex objects still have some limitations:

Frontend only has a basic routing; for your custom tasks, such as saving, you need your own implementation

Basically yes. I think I start using the database plugins. I had hoped using Grav a database solution was not necessary.

How would database plugin help you there? You’d still need to implement custom save solution.

I will follow this tutorial. Not difficult. But still a database solution. Something i didn’t want.

https://truth2say.blogspot.com/2019/01/tutorial-on-using-sqlite-on-grav.html

I probably don’t understand your problem at all. You asked how to save form data to JSON. I answered, but then you say that’s not what you actually need :confused: You need to read the data and not save it… I’m confused

Sorry that I comfused you. Probably I did not explain my case very well.
An ultimate try. My idea was:

  • a frontend form must generate a json file with records.
  • with an another admin form (created with flex) you can edit, delete these records from the created json file.
    Thats all.

@epb,

a frontend form must generate a json file with records.

Any front-end request can be caught by a plugin which writes the posted form into a json file:

$file = File::instance($filename);
$file->save(json_encode($data));

For a complete example you can take a look at the Comments plugin which writes the contents of a comments form into a Yaml file.

This is exactly what I am looking for. Thanks.