rest api – How to store and return json in a (custom) post meta field

Question

I have a custom post type which stores 2 meta fields. The first meta field is string data. No problem. The second is JSON data stored in field let’s call it ‘video’. For example I want to store in this field something like this: {test: data}. The Rest API should understand that this is JSON and return meta: {_string_field: "string", video: {test: data}. In fact it returns: meta: {_string_field: "string", video: '{test: data}'. Notice the ‘. It treats it as a string.

OK. So I understand I need to register the meta field as an Object type. (Object means JSON). AND I need to provide a schema for my JSON. This is explained here under Object Meta Type.

So I follow the instructions and end up with something like this (in register_post_meta):

'show_in_rest' => array(
            'schema' => array(
                'type'       => 'object',
                'properties' => array(
                    'video' => array(
                        'type' => 'string',
                    ),
                  
                ),
            ),
        ),
        'single' => true,
        'type' => 'object',

However – what happens now is the Rest API returns: meta: {_string_field: 'abc', _video: null}

Where null is I want my JSON! What am I doing wrong? What should I be looking for?

0
Kropotkin 4 months 2022-03-28T02:36:09-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse