JSONForms removes keys when deleting the value

Hi, I’m having an issue with the JSONForms library when clearing a field with the x button. The problem is that when I delete the data, the field is automatically deleted. Is this intentional on the part of the library?

Below, I’ll give you more context about my app.

We have an app where JSON files are loaded and divided into “sections.” This way, we have shorter forms divided into topics.

This is our structure:

<JsonForms
schema={schema.properties?.[selectedSection]}
data={currentData}
renderers={customRenderers}
cells={materialCells}
onChange={({ data: updatedData }) => handleJsonFormsChange(updatedData)}
/>

const handleJsonFormsChange = (updatedData) => {
  if (JSON.stringify(workingConfig) !== JSON.stringify(updatedData)) {
    setWorkingConfig((prevConfig) => ({
      ...prevConfig,
      [selectedSection]: updatedData,
    }));

    updateVisorConfigJson({
      ...workingConfig,
      [selectedSection]: updatedData,
    });
  }
};

So, when someone deletes all the string field or push the “x”, the onChange callback executes and returns the updated json deleting the key:value modified. This behaviour brokes our functionality becouse we need allow empty key:value fields.

Here’s an example:

If you need more information, I’ll provide it.

Hi @afcirillo96,

In the past we stored the empty string '' in case a user emptied their input and removed it completely in case they used the clear button. This was not very intuitive and most users seemed to prefer always deleting the value, which is why this is the “new” default for some years now.

To change this behavior you can use different approaches:

  • You can implement a custom string renderer which stores the empty string instead of undefined in case the value is cleared or the clear button is pressed, Or
  • You can implement a JSON Forms middleware which adjust a string value to be the empty string instead of undefined in case an action is performed which sets a string value to undefined, Or
  • You specify an empty string default value for all string properties in JSON Schema and activate the default support of AJV