简体   繁体   中英

How to convert PHP object to JSON fromat for OpenAPI 3

I'm currently working on API for PHP project using OpenAPI 3.0.2 . The problem is that I've got the nested php array according to which I want to describe an OpenAPI Schema and send this data as JSON inside POST request body for testing.

Here is the PHP data I want to convert to JSON:

    [[
        'question'    => 'Do you have an active mobile money account?'
        'answers'     => [
            [
                'label'  => 'A.',
                'text'   => 'Yes',
                'custom' => false,
                'value'  => 'yes',
            ],
            [
                'label'  => 'B.',
                'text'   => 'No',
                'custom' => false,
                'value'  => 'no',
            ]
        ],
    ],...]

And here is a JSON which I've already tried to make, but it didn't match the data object (server's validation failed)

openapi.json

"example":{
    "survey": [
                {
                  "question": "mobile money account",
                  "answers": [
                    [[{"label":"A."},{"text": "Yes"},{"custom": false},{"value":"yes"}]]
                  ]
                },
     ...
}

NOTE! The validation fails only on "answers" property!

EDITED: using json_decode($arr,true) gives me the following:

"answers": [
                {
                  "label": "A.",
                  "text": "Yes",
                  "custom": false,
                  "value": "yes"
                },
                {
                  "label": "B.",
                  "text": "No",
                  "custom": false,
                  "value": "no"
                }
              ]

But the server still responds with an error that "answers" are invalid

Each object in JSON corresponds to a separate associative array (or PHP object, but we're going to talk about arrays here).

So to get such JSON "answer" attribute's value:

[
     [[{"label":"A."},{"text": "Yes"},{"custom": false},{"value":"yes"}]]
]

We need an "array of arrays of arrays of associative arrays".

So in PHP it should be:

 [[[
    ["label" => "A."],
    ["text" => "Yes"],
    ["custom" => false],
    ["value" => "yes"]
]]]

Note For me it looks like too many nesting in the expected structure, but whatever - this is what you said you want.

The stuff you show us as "output of json_encode" is not the real output of json_encode. It would never show you an associative array because it does not exist in JSON. It would never give you something like [ 'label' => 'A.', .

I fed the answers to json_encode and here is what I've got:

"'answers' => [ [ 'label' => 'A.', 'text' => 'Less than 500 UGX', 'custom' => false, 'value' => '< 500 UGX', ]] "

This is wrong . It's not the output of json_encode. See my code below.

I think the problem is in the dimensions of answers . You array seems too deep:

"answers": [ [[

Notice 3 opening array brackets?


$x = [[
        'question'    => 'Do you have an active mobile money account?',
        'answers'     => [
            [
                'label'  => 'A.',
                'text'   => 'Yes',
                'custom' => false,
                'value'  => 'yes',
            ],
            [
                'label'  => 'B.',
                'text'   => 'No',
                'custom' => false,
                'value'  => 'no',
            ]
        ]
]];

echo json_encode($x, JSON_PRETTY_PRINT);

// Output:

[
    {
        "question": "Do you have an active mobile money account?",
        "answers": [
            {
                "label": "A.",
                "text": "Yes",
                "custom": false,
                "value": "yes"
            },
            {
                "label": "B.",
                "text": "No",
                "custom": false,
                "value": "no"
            }
        ]
    }
]

Show your full php array data. If it is like this -

$d =  [[
    'question'    => 'Do you have an active mobile money account?',
    'answers'     => [
        [
            'label'  => 'A.',
            'text'   => 'Yes',
            'custom' => false,
            'value'  => 'yes',
        ],
        [
            'label'  => 'B.',
            'text'   => 'No',
            'custom' => false,
            'value'  => 'no',
        ]
    ],
],

];

Then you need to use json_encode($d,true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM