#json #migration #drupal-8 #jmespath
#json #миграция #drupal-8 #jmespath
Вопрос:
У меня есть блок json, который выглядит как показано ниже. Я использую библиотеку Jmespath PHP для получения категорий из приведенного ниже ввода JSON. Я использую следующий запрос(_element[].[name, отображаемое имя, self.uri]) для возврата родительских категорий в простом массиве JSON, но он не включает дочерние категории, которые находятся внутри _element[]._child[].[имя, отображаемое имя, self.uri] Как я могу объединить два запроса в один для извлечения записей?
Примечание: Внутри _child также может быть _child, т.Е. Глубина _child может составлять до 5 внутри _element. Т.е. _element[]._child[].дочерний элемент[].
{
"self": {
"type": "navigations.navigations",
"uri": "/navigations/cf?zoom=element,element:child,element:child:child,element:child:child:child,element:child:child:child:child",
"href": "https://www.example.com/navigations/cf?zoom=element,element:child,element:child:child,element:child:child:child,element:child:child:child:child"
},
"messages": [],
"links": [
{
"rel": "element",
"rev": "list",
"type": "navigations.navigation",
"uri": "/navigations/cf/mntf643nmfzhi4din5xgk4y=",
"href": "https://www.example.com/navigations/cf/mntf643nmfzhi4din5xgk4y="
},
{
"rel": "element",
"rev": "list",
"type": "navigations.navigation",
"uri": "/navigations/cf/mntf6y3bnvsxeylt=",
"href": "https://www.example.com/navigations/cf/mntf6y3bnvsxeylt="
},
{
"rel": "element",
"rev": "list",
"type": "navigations.navigation",
"uri": "/navigations/cf/mnzv643foj3gsy3fom=",
"href": "https://www.example.com/navigations/cf/mnzv643foj3gsy3fom="
},
{
"rel": "element",
"rev": "list",
"type": "navigations.navigation",
"uri": "/navigations/cf/mfyha3djmnqw4y3fonsxe5tjmnsxg=",
"href": "https://www.example.com/navigations/cf/mfyha3djmnqw4y3fonsxe5tjmnsxg="
}
],
"_element": [
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/mntf643nmfzhi4din5xgk4y=",
"href": "https://www.example.com/navigations/cf/mntf643nmfzhi4din5xgk4y="
},
"messages": [],
"links": [
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "child",
"rev": "parent",
"type": "navigations.navigation",
"uri": "/navigations/cf/nfxxg=",
"href": "https://www.example.com/navigations/cf/nfxxg="
},
{
"rel": "child",
"rev": "parent",
"type": "navigations.navigation",
"uri": "/navigations/cf/ifxgi4tjn5sa=",
"href": "https://www.example.com/navigations/cf/ifxgi4tjn5sa="
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/mntf643nmfzhi4din5xgk4y=/1",
"href": "https://www.example.com/searches/navigations/cf/mntf643nmfzhi4din5xgk4y=/1"
}
],
"_child": [
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/nfxxg=",
"href": "https://www.example.com/navigations/cf/nfxxg="
},
"messages": [],
"links": [
{
"rel": "parent",
"rev": "child",
"type": "navigations.navigation",
"uri": "/navigations/cf/mntf643nmfzhi4din5xgk4y=",
"href": "https://www.example.com/navigations/cf/mntf643nmfzhi4din5xgk4y="
},
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/nfxxg=/1",
"href": "https://www.example.com/searches/navigations/cf/nfxxg=/1"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "IOS",
"name": "catDescription",
"value": "IOS"
},
{
"display-name": "Name",
"display-value": "IOS",
"name": "catName",
"value": "IOS"
}
],
"display-name": "IOS",
"name": "ios"
},
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/ifxgi4tjn5sa=",
"href": "https://www.example.com/navigations/cf/ifxgi4tjn5sa="
},
"messages": [],
"links": [
{
"rel": "parent",
"rev": "child",
"type": "navigations.navigation",
"uri": "/navigations/cf/mntf643nmfzhi4din5xgk4y=",
"href": "https://www.example.com/navigations/cf/mntf643nmfzhi4din5xgk4y="
},
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/ifxgi4tjn5sa=/1",
"href": "https://www.example.com/searches/navigations/cf/ifxgi4tjn5sa=/1"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Andriod cellphones",
"name": "catDescription",
"value": "Andriod cellphones"
},
{
"display-name": "Name",
"display-value": "Andriod cellphones",
"name": "catName",
"value": "Andriod cellphones"
}
],
"display-name": "Andriod",
"name": "Andriod"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Smartphones",
"name": "catDescription",
"value": "Smartphones"
},
{
"display-name": "Name",
"display-value": "Smartphones",
"name": "catName",
"value": "Smartphones"
}
],
"display-name": "Smartphones",
"name": "cf_smartphones"
},
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/mntf6y3bnvsxeylt=",
"href": "https://www.example.com/navigations/cf/mntf6y3bnvsxeylt="
},
"messages": [],
"links": [
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "child",
"rev": "parent",
"type": "navigations.navigation",
"uri": "/navigations/cf/irgfeu2mki=",
"href": "https://www.example.com/navigations/cf/irgfeu2mki="
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/mntf6y3bnvsxeylt=/1",
"href": "https://www.example.com/searches/navigations/cf/mntf6y3bnvsxeylt=/1"
}
],
"_child": [
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/irgfeu2mki=",
"href": "https://www.example.com/navigations/cf/irgfeu2mki="
},
"messages": [],
"links": [
{
"rel": "parent",
"rev": "child",
"type": "navigations.navigation",
"uri": "/navigations/cf/mntf6y3bnvsxeylt=",
"href": "https://www.example.com/navigations/cf/mntf6y3bnvsxeylt="
},
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/irgfeu2mki=/1",
"href": "https://www.example.com/searches/navigations/cf/irgfeu2mki=/1"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "DLR SLR Cameras",
"name": "catDescription",
"value": "DLR SLR Cameras"
},
{
"display-name": "Name",
"display-value": "DLR SLR Cameras",
"name": "catName",
"value": "DLR SLR Cameras"
}
],
"display-name": "Digital SLR",
"name": "DLRSLR"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Cameras",
"name": "catDescription",
"value": "Cameras"
},
{
"display-name": "Name",
"display-value": "Cameras",
"name": "catName",
"value": "Cameras"
}
],
"display-name": "Cameras",
"name": "cf_cameras"
},
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/mnzv643foj3gsy3fom=",
"href": "https://www.example.com/navigations/cf/mnzv643foj3gsy3fom="
},
"messages": [],
"links": [
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "child",
"rev": "parent",
"type": "navigations.navigation",
"uri": "/navigations/cf/obzg6ztfonzws33omfwhizldna=",
"href": "https://www.example.com/navigations/cf/obzg6ztfonzws33omfwhizldna="
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/mnzv643foj3gsy3fom=/1",
"href": "https://www.example.com/searches/navigations/cf/mnzv643foj3gsy3fom=/1"
}
],
"_child": [
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/obzg6ztfonzws33omfwhizldna=",
"href": "https://www.example.com/navigations/cf/obzg6ztfonzws33omfwhizldna="
},
"messages": [],
"links": [
{
"rel": "parent",
"rev": "child",
"type": "navigations.navigation",
"uri": "/navigations/cf/mnzv643foj3gsy3fom=",
"href": "https://www.example.com/navigations/cf/mnzv643foj3gsy3fom="
},
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "child",
"rev": "parent",
"type": "navigations.navigation",
"uri": "/navigations/cf/mrqxiylsmvrw65tfoj4q=",
"href": "https://www.example.com/navigations/cf/mrqxiylsmvrw65tfoj4q="
},
{
"rel": "child",
"rev": "parent",
"type": "navigations.navigation",
"uri": "/navigations/cf/ozuxe5ltm52wc4te=",
"href": "https://www.example.com/navigations/cf/ozuxe5ltm52wc4te="
},
{
"rel": "child",
"rev": "parent",
"type": "navigations.navigation",
"uri": "/navigations/cf/mrqxiyluojqw443gmvza=",
"href": "https://www.example.com/navigations/cf/mrqxiyluojqw443gmvza="
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/obzg6ztfonzws33omfwhizldna=/1",
"href": "https://www.example.com/searches/navigations/cf/obzg6ztfonzws33omfwhizldna=/1"
}
],
"_child": [
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/mrqxiylsmvrw65tfoj4q=",
"href": "https://www.example.com/navigations/cf/mrqxiylsmvrw65tfoj4q="
},
"messages": [],
"links": [
{
"rel": "parent",
"rev": "child",
"type": "navigations.navigation",
"uri": "/navigations/cf/obzg6ztfonzws33omfwhizldna=",
"href": "https://www.example.com/navigations/cf/obzg6ztfonzws33omfwhizldna="
},
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/mrqxiylsmvrw65tfoj4q=/1",
"href": "https://www.example.com/searches/navigations/cf/mrqxiylsmvrw65tfoj4q=/1"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Data Recovery",
"name": "catDescription",
"value": "Data Recovery"
},
{
"display-name": "Name",
"display-value": "Data Recovery",
"name": "catName",
"value": "Data Recovery"
}
],
"display-name": "Data Recovery",
"name": "datarecovery"
},
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/ozuxe5ltm52wc4te=",
"href": "https://www.example.com/navigations/cf/ozuxe5ltm52wc4te="
},
"messages": [],
"links": [
{
"rel": "parent",
"rev": "child",
"type": "navigations.navigation",
"uri": "/navigations/cf/obzg6ztfonzws33omfwhizldna=",
"href": "https://www.example.com/navigations/cf/obzg6ztfonzws33omfwhizldna="
},
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/ozuxe5ltm52wc4te=/1",
"href": "https://www.example.com/searches/navigations/cf/ozuxe5ltm52wc4te=/1"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Virus Guard",
"name": "catDescription",
"value": "Virus Guard"
},
{
"display-name": "Name",
"display-value": "Virus Guard",
"name": "catName",
"value": "Virus Guard"
}
],
"display-name": "Virus Guard",
"name": "virusguard"
},
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/mrqxiyluojqw443gmvza=",
"href": "https://www.example.com/navigations/cf/mrqxiyluojqw443gmvza="
},
"messages": [],
"links": [
{
"rel": "parent",
"rev": "child",
"type": "navigations.navigation",
"uri": "/navigations/cf/obzg6ztfonzws33omfwhizldna=",
"href": "https://www.example.com/navigations/cf/obzg6ztfonzws33omfwhizldna="
},
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/mrqxiyluojqw443gmvza=/1",
"href": "https://www.example.com/searches/navigations/cf/mrqxiyluojqw443gmvza=/1"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Data Transfer",
"name": "catDescription",
"value": "Data Transfer"
},
{
"display-name": "Name",
"display-value": "Data Transfer",
"name": "catName",
"value": "Data Transfer"
}
],
"display-name": "Data Transfer",
"name": "datatransfer"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Professional Tech",
"name": "catDescription",
"value": "Professional Tech"
},
{
"display-name": "Name",
"display-value": "Professional Tech",
"name": "catName",
"value": "Professional Tech"
}
],
"display-name": "Professional Tech",
"name": "professionaltech"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Services",
"name": "catDescription",
"value": "Services"
},
{
"display-name": "Name",
"display-value": "Services",
"name": "catName",
"value": "Services"
}
],
"display-name": "Services",
"name": "cs_services"
},
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/mfyha3djmnqw4y3fonsxe5tjmnsxg=",
"href": "https://www.example.com/navigations/cf/mfyha3djmnqw4y3fonsxe5tjmnsxg="
},
"messages": [],
"links": [
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "child",
"rev": "parent",
"type": "navigations.navigation",
"uri": "/navigations/cf/obzg6ztfonzws33omfwhgzlsozuwgzlt=",
"href": "https://www.example.com/navigations/cf/obzg6ztfonzws33omfwhgzlsozuwgzlt="
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/mfyha3djmnqw4y3fonsxe5tjmnsxg=/1",
"href": "https://www.example.com/searches/navigations/cf/mfyha3djmnqw4y3fonsxe5tjmnsxg=/1"
}
],
"_child": [
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/obzg6ztfonzws33omfwhgzlsozuwgzlt=",
"href": "https://www.example.com/navigations/cf/obzg6ztfonzws33omfwhgzlsozuwgzlt="
},
"messages": [],
"links": [
{
"rel": "parent",
"rev": "child",
"type": "navigations.navigation",
"uri": "/navigations/cf/mfyha3djmnqw4y3fonsxe5tjmnsxg=",
"href": "https://www.example.com/navigations/cf/mfyha3djmnqw4y3fonsxe5tjmnsxg="
},
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "child",
"rev": "parent",
"type": "navigations.navigation",
"uri": "/navigations/cf/mrswy2lwmvzhs4dmovzws3ttorqwy3dboruw63q=",
"href": "https://www.example.com/navigations/cf/mrswy2lwmvzhs4dmovzws3ttorqwy3dboruw63q="
},
{
"rel": "child",
"rev": "parent",
"type": "navigations.navigation",
"uri": "/navigations/cf/mrswy2lwmvzhs=",
"href": "https://www.example.com/navigations/cf/mrswy2lwmvzhs="
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/obzg6ztfonzws33omfwhgzlsozuwgzlt=/1",
"href": "https://www.example.com/searches/navigations/cf/obzg6ztfonzws33omfwhgzlsozuwgzlt=/1"
}
],
"_child": [
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/mrswy2lwmvzhs4dmovzws3ttorqwy3dboruw63q=",
"href": "https://www.example.com/navigations/cf/mrswy2lwmvzhs4dmovzws3ttorqwy3dboruw63q="
},
"messages": [],
"links": [
{
"rel": "parent",
"rev": "child",
"type": "navigations.navigation",
"uri": "/navigations/cf/obzg6ztfonzws33omfwhgzlsozuwgzlt=",
"href": "https://www.example.com/navigations/cf/obzg6ztfonzws33omfwhgzlsozuwgzlt="
},
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/mrswy2lwmvzhs4dmovzws3ttorqwy3dboruw63q=/1",
"href": "https://www.example.com/searches/navigations/cf/mrswy2lwmvzhs4dmovzws3ttorqwy3dboruw63q=/1"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Delivery Installation",
"name": "catDescription",
"value": "Delivery Installation"
},
{
"display-name": "Name",
"display-value": "Delivery Installation",
"name": "catName",
"value": "Delivery Installation"
}
],
"display-name": "Delivery Installation",
"name": "deliveryplusinstallation"
},
{
"self": {
"type": "navigations.navigation",
"uri": "/navigations/cf/mrswy2lwmvzhs=",
"href": "https://www.example.com/navigations/cf/mrswy2lwmvzhs="
},
"messages": [],
"links": [
{
"rel": "parent",
"rev": "child",
"type": "navigations.navigation",
"uri": "/navigations/cf/obzg6ztfonzws33omfwhgzlsozuwgzlt=",
"href": "https://www.example.com/navigations/cf/obzg6ztfonzws33omfwhgzlsozuwgzlt="
},
{
"rel": "top",
"type": "navigations.navigations",
"uri": "/navigations/cf",
"href": "https://www.example.com/navigations/cf"
},
{
"rel": "items",
"type": "searches.navigation-search-result",
"uri": "/searches/navigations/cf/mrswy2lwmvzhs=/1",
"href": "https://www.example.com/searches/navigations/cf/mrswy2lwmvzhs=/1"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Delivery",
"name": "catDescription",
"value": "Delivery"
},
{
"display-name": "Name",
"display-value": "Delivery",
"name": "catName",
"value": "Delivery"
}
],
"display-name": "Delivery",
"name": "delivery"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Professional Services",
"name": "catDescription",
"value": "Professional Services"
},
{
"display-name": "Name",
"display-value": "Professional Services",
"name": "catName",
"value": "Professional Services"
}
],
"display-name": "Professional Services",
"name": "professionalservices"
}
],
"details": [
{
"display-name": "Category Description",
"display-value": "Applicance Services",
"name": "catDescription",
"value": "Applicance Services"
},
{
"display-name": "Name",
"display-value": "Applicance Services",
"name": "catName",
"value": "Applicance Services"
}
],
"display-name": "Applicance Services",
"name": "applicanceservices"
}
]
}
Ответ №1:
Я не знаю, лучший ли это способ сделать это, но вот мой ответ:
_element[*].[{name: name, display-name: display-name, URI: self.uri},_child[*].{name: name, display-name: display-name, URI: self.uri}[],_child[*]._child[][].{name: name, display-name: display-name, URI: self.uri}][][]
Вы получите этот результат:
[
{
"name": "cf_smartphones",
"display-name": "Smartphones",
"URI": "/navigations/cf/mntf643nmfzhi4din5xgk4y="
},
{
"name": "ios",
"display-name": "IOS",
"URI": "/navigations/cf/nfxxg="
},
{
"name": "Andriod",
"display-name": "Andriod",
"URI": "/navigations/cf/ifxgi4tjn5sa="
},
{
"name": "cf_cameras",
"display-name": "Cameras",
"URI": "/navigations/cf/mntf6y3bnvsxeylt="
},
{
"name": "DLRSLR",
"display-name": "Digital SLR",
"URI": "/navigations/cf/irgfeu2mki="
},
{
"name": "cs_services",
"display-name": "Services",
"URI": "/navigations/cf/mnzv643foj3gsy3fom="
},
{
"name": "professionaltech",
"display-name": "Professional Tech",
"URI": "/navigations/cf/obzg6ztfonzws33omfwhizldna="
},
{
"name": "datarecovery",
"display-name": "Data Recovery",
"URI": "/navigations/cf/mrqxiylsmvrw65tfoj4q="
},
{
"name": "virusguard",
"display-name": "Virus Guard",
"URI": "/navigations/cf/ozuxe5ltm52wc4te="
},
{
"name": "datatransfer",
"display-name": "Data Transfer",
"URI": "/navigations/cf/mrqxiyluojqw443gmvza="
},
{
"name": "applicanceservices",
"display-name": "Applicance Services",
"URI": "/navigations/cf/mfyha3djmnqw4y3fonsxe5tjmnsxg="
},
{
"name": "professionalservices",
"display-name": "Professional Services",
"URI": "/navigations/cf/obzg6ztfonzws33omfwhgzlsozuwgzlt="
},
{
"name": "deliveryplusinstallation",
"display-name": "Delivery Installation",
"URI": "/navigations/cf/mrswy2lwmvzhs4dmovzws3ttorqwy3dboruw63q="
},
{
"name": "delivery",
"display-name": "Delivery",
"URI": "/navigations/cf/mrswy2lwmvzhs="
}
]