EDIT: added the yml and fixed the json data (was a copy/paste error)
Problem: Not able to get the data (id or Name) from the "Sets" array. Goal is to create 4 contents out of this data.
This is the source:
{
"Time" : "2019-10-08",
"Centers" : [ {
"id" : "B01502",
"CenterId" : 1502,
"Sets" : [ {
"id" : "B0150200",
"Name" : "name_valuex"
}, {
"id" : "B0150201",
"Name" : "name_valueu"
} ]
}, {
"id" : "B01503",
"CenterId" : 1503,
"Sets" : [ {
"id" : "B0150301",
"Name" : "name_valueb"
}, {
"id" : "B0150302",
"Name" : "name_valuea"
} ]
} ]
}
This is my YML
id: test_data
source:
constants:
TITLE_PREFIX: 'title prefix - '
plugin: url
data_fetcher_plugin: file
data_parser_plugin: json
urls:
- /test.json
item_selector: 1
fields:
- name: id
label: 'id'
selector: id
ids:
id:
type: string
process:
title: constants/TITLE_PREFIX
field_presetid: id
destination:
plugin: 'entity:node'
default_bundle: bundle_name
I have tried to put different values to "item_selector:" like /Centers/Sets or "0" etc.
Modules:
Migrate: Version: 8.7.8
Migrate Plus: Version: 8.x-4.2
Migrate Tools: Version: 8.x-4.4
Drupal: 8.7.8
PHP 7.2.22
Comments
Comment #2
jukka792 commentedComment #3
jukka792 commentedComment #4
jukka792 commentedComment #5
heddnCreate a custom source plugin that parses the source and combines all the values into a single array. Use of a PHP generator (yield) is encouraged.
Comment #6
marvil07 commentedComment #8
marvil07 commentedIt will be nice to have a way to walk through a set of items nested in all the item selector items.
I have started one custom data parser plugin that does that.
It basically extends the json data parser plugin, and then uses a given selector to unify its values across all the elements.
E.g. the following the original example, the following source configuration bits will extract the four items.
On the ideal world, jq pipelines can do the work once, but if the source endpoints cannot be changed, something like this data parser helps to produce a set that is sensible to walk.
New commits on new
3087614-json-subitemstopic branch and related new MR !87.Comment #9
init90I'm using the code above and it works well.
Comment #11
slbrassard commentedThe parser was not working when using a pager because getSourceData always assumed we wanted the subitems. I added a check for the item selector parameter being the data item selector before getting subitems. I also removed the getSourceData function return type since it could be anything as with the parent function.
Comment #13
heddnThxs for the contributions