We are importing from a JSON file where not every row has every source field defined in the migration. For example, not all addresses have an "address-line-2" field.

This works perfectly fine - all the nodes are imported correctly, but every time there is a missing source field in the JSON file, it creates an "Undefined Index..." notice from src/Plugin/migrate_plus/data_parser/Json.php line 122. This wasn't really an issue until we set it up to import the migration during cron runs. Then all the notices started showing up on-screen in the site itself, which we don't want.

I was able to get rid of the notices by putting in an "array_key_exists" check (see patch), but I'm not completely convinced that is the best way to go. Any help would be appreciated.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Amerie created an issue. See original summary.

leewbutler’s picture

Having the same issue however in my JSON the entire array value is missing in certain records - so I had to adjust your patch to check for that scenario as well. The addition of "is_array($field_data)" to the "if" worked. Here's the patch with that adjustment.

heddn’s picture

Status: Active » Needs work
Issue tags: +Needs tests

I'd appreciate if we could add a unit test or two to test the different scenarios.

arnested’s picture

I just added this patch and it does what it says on the tin. Thumbs up.

SylvainM’s picture

Test added.

I changed also a line to match coding standards:

} else {

must be:

}
else {

SylvainM’s picture

  • heddn committed 8a296ba on 8.x-4.x authored by SylvainM
    Issue #2906264 by SylvainM, Amerie, leewbutler, heddn: Undefined index...
heddn’s picture

Status: Needs review » Fixed
+++ b/src/Plugin/migrate_plus/data_parser/Json.php
@@ -119,7 +119,12 @@ protected function fetchNextRow() {
+            $field_data = "";

Nit: Typically I see single quotes (') not double. That an whitespace I fixed on commit. Thanks for all your contributions!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.