Skip to content

Instantly share code, notes, and snippets.

@dinarcon
Last active July 3, 2022 11:03
Show Gist options
  • Save dinarcon/72922e8cf634ada47483117ffa659d76 to your computer and use it in GitHub Desktop.
Save dinarcon/72922e8cf634ada47483117ffa659d76 to your computer and use it in GitHub Desktop.
Drupal 8 JSON Migration Example
# This is the *third* configuration to import with configuration type: 'Migration'
# This migration demonstrates importing from a monolithic JSON file.
# Forked from https://github.com/heddn/json_example_migrate/blob/json/web/modules/custom/custom_migrate/config/install/migrate_plus.migration.omdb_json.yml
uuid: b113ad9f-1ed7-43e5-802c-0e0270d2b7fa
id: omdb_json_article
label: JSON feed of movies (Article)
migration_group: json_example
source:
# We use the JSON source plugin.
plugin: url
data_fetcher_plugin: http
data_parser_plugin: json
# The data_parser normally limits the fields passed on to the source plugin
# to fields configured to be used as part of the migration. To support more
# dynamic migrations, the JSON data parser supports including the original
# data for the current row. Simply include the 'include_raw_data' flag set
# to `true` to enable this. This option is disabled by default to minimize
# memory footprint for migrations that do not need this capability.
include_raw_data: true
# The 'urls' setting is required, but in this case it is being provided by
# the group as part of the shared_configuration.
# urls: 'https://www.omdbapi.com/?s=space&type=movie&r=json&apikey=86e4b169'
# An xpath-like selector corresponding to the items to be imported.
item_selector: Search
# Under 'fields', we list the data items to be imported. The first level keys
# are the source field names we want to populate (the names to be used as
# sources in the process configuration below). For each field we're importing,
# we provide a label (optional - this is for display in migration tools) and
# an xpath for retrieving that value. It's important to note that this xpath
# is relative to the elements retrieved by item_selector.
fields:
-
name: movieName
label: 'Movie Name'
selector: Title
-
name: year
label: 'Publish Year'
selector: Year
-
name: imdbID
label: 'IMDB ID'
selector: imdbID
-
name: poster
label: 'Image URL to a movie poster'
selector: Poster
# Under 'ids', we identify source fields populated above which will uniquely
# identify each imported item. The 'type' makes sure the migration map table
# uses the proper schema type for stored the IDs.
ids:
imdbID:
type: string
constants:
BUNDLE: article
DESTINATION_DIRECTORY: public://posters/
FILE_EXTENSION: .jpg
process:
# Note that the source field names here (title and year) were
# defined by the 'fields' configuration for the source plugin above.
type: constants/BUNDLE
title:
plugin: concat
source:
- movieName
- year
delimiter: ' - '
body/value: imdbID
body/format:
plugin: default_value
default_value: basic_html
destination_path:
plugin: concat
source:
- constants/DESTINATION_DIRECTORY
- imdbID
- constants/FILE_EXTENSION
field_image/target_id:
-
plugin: file_copy
source:
- poster
- '@destination_path'
-
plugin: entity_generate
value_key: uri
entity_type: file
field_image/alt: '@title'
field_image/title: '@title'
destination:
plugin: entity:node
# This is the *second* configuration to import with configuration type: 'Migration'
uuid: adcca1ff-087e-4742-912a-7d5117813ddf
id: omdb_json_basic
label: JSON feed of movies (Basic page)
migration_group: json_example
source:
plugin: url
data_fetcher_plugin: http
data_parser_plugin: json
include_raw_data: true
# The 'urls' setting is required, but in this case it is being provided by
# the group as part of the shared_configuration.
# urls: 'https://www.omdbapi.com/?s=space&type=movie&r=json&apikey=86e4b169'
item_selector: Search
fields:
-
name: movieName
label: 'Movie Name'
selector: Title
-
name: imdbID
label: 'IMDB ID'
selector: imdbID
ids:
imdbID:
type: string
destination:
plugin: entity:node
process:
type:
plugin: default_value
default_value: page
title: movieName
# This is the *first* configuration to import with configuration type: 'Migration Group'
uuid: 83170e08-b848-40be-898e-c78c9b12a1f9
langcode: en
status: true
dependencies: { }
id: json_example
label: JSON Example
description: ''
source_type: ''
module: null
shared_configuration:
source:
urls: 'https://www.omdbapi.com/?s=space&type=movie&r=json&apikey=86e4b169'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment