• Resolved radkosedlak

    (@radkosedlak)


    Hello,

    Is there a way to change structure of JSON created by your plugin?

    This is what I have:

    {
     "email": "xyz@google.cz",
     "mobile": "+42600003456",
     "main_company": "ABC a.s.",
     "your-message": "test text"
    }

    And this is what I need:

    {
        "overwrite": true,
        "contact_tags_overwrite": true,
        "overwrite_companies": true,
        "company_tags_overwrite": true,
        "data": [
            {
                "Contact": {
                    "email": "xyz@google.cz",
                    "mobile": "+42600003456",
                    "main_company": "ABC a.s.",
                    "your-message": "test text"
                },
            }
        ]
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mário Valney

    (@mariovalney)

    Yes. You can use the same filter from another question to change the BODY.

    Or another filter: ctz_get_data_from_contact_form. In this cenario, you can change the data before the request function.

    Here is a example:
    https://gist.github.com/mariovalney/7dc48fefa8bed30e102657f509a006ac

    Thread Starter radkosedlak

    (@radkosedlak)

    Thank you Mario,

    I am still new to PHP, so sorry if I ask “not so smart” questions.

    Thank you for link provided. I figured out something out of it. Right now my JSON looks like this:

    {
        "data": [
            {
                "Contact": {
                    "email": "xyz@google.cz",
                    "mobile": "+42600003456",
                    "main_company": "ABC a.s.",
                    "your-message": "test text"
                },
            }
        ]
    }

    But I still need to add those fields:
    “overwrite”: true,
    “contact_tags_overwrite”: true,
    “overwrite_companies”: true,
    “company_tags_overwrite”: true,
    above “data” to the JSON body part.

    This is my PHP function:

    add_filter( 'ctz_post_request_args', 'radkosedlak_ctz_post_request_args' );
    function radkosedlak_ctz_post_request_args( $args ) {
        $args['method'] = 'PUT';
        $args['headers']['Authorization'] = 'Token XXXX';
        $body = json_decode( $args['body'] );
        $body = array(
            'data' => array(
                array(
                    'Contact' => $body,
                ),
            ),
        );
    
        $args['body'] = json_encode( $body );
    
        return $args;
    }

    Could you please help me out of this? I promise, that after that I’m going to give you a break 🙂

    Plugin Author Mário Valney

    (@mariovalney)

    You can add key->value pairs to your array.

    Like this:

    $body = array(
        "overwrite" => true,
        "contact_tags_overwrite" => true,
        "overwrite_companies" => true,
        "company_tags_overwrite" => true,
        'data' => array(
        // ...
    );
    Thread Starter radkosedlak

    (@radkosedlak)

    That’s it. Thank you! <3

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change JSON structure’ is closed to new replies.