Receiving value of custom field as JSON in API and adminUI

System Information
  • Strapi Version: 3.4.1
  • Operating System: Docker
  • Database: mySQL 5.7.32
  • Node Version: 14.15.3
  • NPM Version: 6.14.9
  • Yarn Version: 1.22.5

Hello :wave:

I registered a custom field via plugin (based on Creating a new Field in the administration panel).

As my field has a quite complex data structure, I did JSON.stringify(newValue) to hand on the value as a stringified object to the content-manager. On receiving the value, I did JSON.parse(value) to work with data. Also I parsed the value in every find or findOne of the controller of the content-type using this custom field in its model.
All good so far and working like a charm.

But I was wondering how JSON data is handled natively in strapi. And if there would be a way to provide interoperability (for example working on the controller does not feel right, can’t this all be configured in the plugin itself?).
I found out that in https://github.com/strapi/strapi/blob/master/packages/strapi-plugin-content-manager/admin/src/components/InputJSON/index.js the returned object on change included a type: 'json'

{
    target: {
        name,
        value,
        type: 'json',
    },
}

So I replaced my value: JSON.stringify({...}) with value, type: 'json' which worked and left processing the input data to strapi. In mySQL the value is saved as a longtext.
But when receiving the value, it is still a string. As I recognized, no line of code in the InputJSON mentioned above parses the value, I guess this must have happened somewhere else.

Where? Or what do I have to do to receive value as JSON object?

Is there something in the fieldAPI to tell that this custom field type should be interpreted like type json?

I think, what I kind of want to achieve is to let strapi/bookshelf handle a custom field like a field of type json in the background.
Is this configurable via the plugin options or somewhere else?

Can I be more precise about the issue? What could help to make a suggestion?

Is there such a feature which can handle custom field types via a native field type handler (for example treating a custom field type like json internally)? Or do I have to take care of it?
Just thought it would be best to rely on strapi and be as close to the original system as possible instead of creating custom (maybe not as performant or secure) solutions.

Hey MathiasDunker,

I was sweating finding some sort of solution but I came across strapi’s lifecycle hooks. You can use these hooks and stringify/parse at root level yourself

hooks such as:
beforeCreate [stringify], afterFind [parse], afterFindOne [parse]

Hey @MatthiasDunker,

Did you find a solution?
I’m working on a custom field for GeoJSON (locality autocomplete), and I have exactly the same problem.

@vemuez I also tried lifecycle hooks but it didn’t work because lifecycle hooks are triggered only for API calls, and not in the admin panel.
In API calls it’s useless for me because we can directly put a JSON inside the payload.

I need absolutely save it as a JSON because I need to use the mongodb index 2dsphere, and it works only on GeoJSON types.

Thanks for your help

Hey @Sygma,

I fell back to manual parsing/stringifying. Still don’t know, if this is meant to be or if there is some strategy provided by strapi.