JSON Feed Throws AJAX Error Because of Expected Array in JsonPathParser

Created on 29 July 2024, 4 months ago
Updated 30 July 2024, 4 months ago

Problem/Motivation

Trying to create a feed to import JSON-formatted calendar event info. Attempts to import result in an Ajax error in the UI. Logs indicate a PHP error in JsonPathParser.php:

TypeError: Drupal\feeds_ex\Feeds\Parser\JsonPathParser::search(): Argument #1 ($data) must be of type array, int given, called in /Applications/MAMP/htdocs/events/web/modules/contrib/feeds_ex/src/Feeds/Parser/JsonPathParser.php on line 53 in Drupal\feeds_ex\Feeds\Parser\JsonPathParser->search() (line 114 of /Applications/MAMP/htdocs/events/web/modules/contrib/feeds_ex/src/Feeds/Parser/JsonPathParser.php).

Simple, single node JSON is the following:

{"event":{"id":46677089246185,"title":"PolishFest 2024","url":null,"updated_at":"2024-06-11T15:54:30-04:00","created_at":"2024-06-11T15:42:29-04:00","facebook_id":null,"first_date":"2024-11-10","last_date":"2024-11-10","hashtag":null,"urlname":"polishfest-2024","user_id":43888401226071,"directions":null,"allows_reviews":true,"allows_attendance":true,"location":"","room_number":null,"location_name":"Cathedral Commons Room","status":"live","experience":"inperson","stream_url":null,"stream_info":null,"stream_embed_code":null,"created_by":43888401226071,"updated_by":43888401226071,"conference_event_id":null,"kind":"standalone","city_id":null,"neighborhood_id":null,"school_id":225,"campus_id":null,"recurring":false,"free":false,"private":false,"verified":true,"rejected":false,"sponsored":false,"venue_id":514395,"ticket_url":null,"ticket_cost":null,"has_register":true,"keywords":[],"tags":[],"description_text":"The 42nd Annual Polishfest is designed to give everyone an opportunity to experience the living Polish, Lithuanian, and Carpatho-Rusyn cultures\n\nThe 42nd Annual Polishfest is designed to give the festival guests, families, and students an opportunity to experience the living cultures of the Polish, Lithuanian and Carpatho-Rusyn Peoples that throughout history were joined, separated and independently are connected. A living legacy presented to teach, to experience, to taste, to try and to have fun.\n\nThis family-oriented event is FREE to everyone and will include many activities such as Polish name writing; Lithuanian angel papercutting demonstration; pierogi / pirohy cooking demonstrations and samples; and Carpatho-Rusyn spinning and lace making; and a pierogi toss.\n\nEvery display, demonstration, and activity will offer an explanation of the cultural history of the tradition.\n\nEntertainment will include Polish folk songs with a violinist; a Lithuanian choir with Bocjai folk songs; Polish Karazula folk songs and folk dancing by the โ€œLajkonikiโ€ Ensemble; Polka dancing; and contemporary Polish music.\n\nIn-Person event","photo_id":46677136863554,"detail_views":15,"address":"Fifth Ave at Bigelow, Pittsburgh, PA, 15260","description":"<p>The 42nd Annual Polishfest is designed to give everyone an opportunity to experience the living Polish, Lithuanian, and Carpatho-Rusyn cultures</p>\n\n<p>The 42nd Annual Polishfest is designed to give the festival guests, families, and students an opportunity to experience the living cultures of the Polish, Lithuanian and Carpatho-Rusyn Peoples that throughout history were joined, separated and independently are connected. A living legacy presented to teach, to experience, to taste, to try and to have fun.</p>\n\n<p>This family-oriented event is FREE to everyone and will include many activities such as Polish name writing; Lithuanian angel papercutting demonstration; pierogi / pirohy cooking demonstrations and samples; and Carpatho-Rusyn spinning and lace making; and a pierogi toss.</p>\n\n<p>Every display, demonstration, and activity will offer an explanation of the cultural history of the tradition.</p>\n\n<p>Entertainment will include Polish folk songs with a violinist; a Lithuanian choir with Bocjai folk songs; Polish Karazula folk songs and folk dancing by the &ldquo;Lajkoniki&rdquo; Ensemble; Polka dancing; and contemporary Polish music.</p>\n\n<p>In-Person event</p>\n","featured":false,"geo":{"latitude":"40.444422","longitude":"-79.95481","street":null,"city":"Pittsburgh","state":"PA","country":"US","zip":"15213"},"filters":{"event_topic":[{"name":"Arts & Culture","id":117195}],"event_types":[{"name":"Festivals","id":132396}]},"custom_fields":{},"localist_url":"https://calendar.pitt.edu/event/polishfest-2024","localist_ics_url":"https://calendar.pitt.edu/event/polishfest-2024.ics","photo_url":"https://localist-images.azureedge.net/photos/46677136863554/huge/fb2fb7c659e46352c080020f6d467a6c88f9c781.jpg","venue_url":"https://calendar.pitt.edu/cathedral_commons_room","departments":[{"id":23875,"name":"Nationality Rooms and Intercultural Exchange Programs"}],"event_instances":[{"event_instance":{"id":46677089247210,"event_id":46677089246185,"start":"2024-11-10T09:00:00-05:00","end":"2024-11-10T16:00:00-05:00","ranking":0.16,"all_day":false,"num_attending":0}}]}}

In my parser mappings, I have $.event.* as the context and id, title, description_text, and departments.name as sources with appropriate target fields. id is set as the unique target.

The PHP error trace in the Drupal logs seems to show that the JsonPathParser is properly identifying the value in id, but I can't figure out the array error.

PHP version is 8.2.20. Drupal is 10.3.1.

Any help is greatly appreciated.

๐Ÿ’ฌ Support request
Status

Closed: works as designed

Component

JSONPath parser

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States mweixel

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @mweixel
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands megachriz

    I tried your configuration and I could reproduce the issue!

    I found out that the configured context doesn't match with the JSON data. Instead of $.event.*, use $.event as context in your case.

    Apparently, $.event.* should be used when your JSON is structured as follows:

    {
      "event": [
        {
          "id": 1234,
          "title": "Foo"
        },
        {
          "id": 1235,
          "title": "Bar"
        },
        {
          "id": 1236,
          "title": "Baz"
        }
      ]
    }
    

    But your JSON looks like this:

    {
      "event": {
        "id": 1234,
        "title": "Foo"
      }
    }
    

    When the context query is executed, it should return the rows to import. If context is $.event.*, then the rows from the first JSON example are:

    1. {
          "id": 1234,
          "title": "Foo"
      }
      
    2. {
          "id": 1235,
          "title": "Bar"
      }
      
    3. {
          "id": 1236,
          "title": "Baz"
      }
      

    From the second example, the rows would be instead this:

    1. 1234
    2. Foo

    And these are not arrays, which a row should be.

    It would be cool though if this would error in a more friendly way instead of "An AJAX HTTP error occurred."

  • Status changed to Closed: works as designed 4 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mweixel

    Thanks, @megachriz! I'm much more familiar with XML. Your super-clear explanation got me reviewing JSON path syntax so that I was able to build out my mappings pretty quickly.

    It would be great, as you say, to trap Ajax/500 errors and to direct the user to review the logs instead, where the real insight into the problem is.

    -- Mark

Production build 0.71.5 2024