JSON import returns error: Undefined property: stdClass

Hello Community,

I import entries via API in JSON Format. For unknown reasons, some fields are found, others return “undefined property”. Can you tell me why?

"fields": {
        "title": "Pacemaker",
        "tagline": "Pacemaker setzt Impulse zur Transformation der Lernkultur.",
        "descriptionShort": "Pacemaker setzt Impulse zur Transformation der Lernkultur. Wir unterstützen Schulen dabei, dynamischen, interaktiven Unterricht gemäß der #4Ks zu gestalten.",
        "about": "Pacemaker setzt Impulse zur Transformation der Lernkultur. Wir unterstützen Schulen dabei, dynamischen, interaktiven Unterricht gemäß der #4Ks zu gestalten und im Zuge dessen #Chancengleichheit durch #FreieBildung zu ermöglichen. \n\nEin entscheidendes Werkzeug dafür sind digitale Hilfsmittel, deren gezielter Einsatz den Unterschied zwischen passiv, konsumierenden und aktiv gestaltenden Schüler:innen machen kann.",

This is the model I use:

foreach ($results as $key => $organisation) {
            $pages[] = [
                'slug'     => Str::slug($organisation->fields->title),
                'template' => 'organisation',
                'model'    => 'organisation',
                'num'      => 0,
                'content'  => [
                    'title'         => $organisation->fields->title,
                    'tagline'       => $organisation->fields->tagline,
                    'email'         => $organisation->fields->email,
                    'website'       => $organisation->fields->website,
                    'legalform'     => $organisation->fields->legalForm,
                    'descriptionShort'   => $organisation->fields->descriptionShort,
                    'about'         => $organisation->fields->about,
                ]
            ];
        }

It returns the errors:
Undefined property: stdClass::$descriptionShort and Undefined property: stdClass::$about

title, tagline, email and website work totally fine.

Looking forward to your reply.
Thanks!

I don’t know your API but you should check which fields are actually nullable or not. To be on the safe side, you can use the null coalescing operator:

'descriptionShort'   => $organisation->fields->descriptionShort ?? null

Perfect - works really well :slight_smile: