Файл json находится рядом с моим сценарием в том же каталоге и содержит «поле заголовка со значением» в начале, см. «Item1, Item2 и Item3» в примере JSON.В конце каждого раздела товаров есть раздел для «сервисов» и заголовок для каждого сервиса, к которому он привязан.Служб может быть сколько угодно или нет, в зависимости от исходного названия элемента.
Все, что я хочу сделать, - это искать один заголовок службы ... то есть "STitle5" И если он существует ...просто вставьте основной заголовок элемента, т.е. Item3 в массив.Исходя из приведенного ниже примера JSON и только что приведенного примера, в массив будут добавлены только Item1 и Item3.
Я пробовал использовать regex несколькими способами, но не могу понять, каквернуться назад и взять определенную строку, если что-то найдено сразу после нее.В JSON могут быть тысячи записей.Больше мне ничего от этого не нужно, поэтому я подумал, что самый простой способ - просто проанализировать JSON как прямой текст.
[{"id_name": "Item1", "informational": {"values": ["werwe", "werwwe", "8", "ewrwrw", "werewrew", "64432.5390625", "64432.55859375", "64432.36328125", "werw werwerw", "2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "fields": ["werwerw", "erwrwr", "wewrewrer", "werrwrwer", "werwerwrw", "werwerewr", "werwrwr", "stuff", "vendor_product", "version"]}, "role": ["Application Server"], "cpu_cores": ["8"], "create_time": "2017-04-03 16:32:27.738432", "mod_timestamp": "2019-06-26T01:17:23.933103+00:00", "title": "Item1", "family": ["dfsfd"], "OS": ["dfdsfsf"], "sdfdsfdsds": "fdsfdsf", "dsfdsfsd": ["64432.5390625", "64432.55859375", "64432.36328125"], "host": ["dfdsfsdfsdfds"], "sdfdsfds": "sdfdsf", "vend": ["sdada"], "permissions": {"delete": true, "write": true, "user": "dsdsds", "group": {"delete": true, "write": true, "read": true}, "read": true}, "sdsdsdsdsds": ["dfsdfdsfdsfsd"], "_version": "3", "sgrp": "default", "object_type": "dfsfs", "mod_by": "user", "mod_time": "2019-06-25 13:09:47.543535", "_user": "user", "environment": ["dfsdfdfsd"], "description": "", "identifier": {"values": ["dfsdfdfdsffdfsdfs"], "fields": ["host"]}, "sdfdsfsfds": ["SMP"], "role": ["operating_system_host"], "mod_source": "REST", "_key": "afderea-be2d-47a6-9f0d-00857ereef6c", "version": ["2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "create_source": "unknown", "services": [{"title": "STitle", "_key": "865defee-d47f-4b8f-9435-bc4ere89e9b1f8d"}, {"title": "STitle2", "_key": "d9d5e231-3841-4376-a295-ea5fere95168482"}, {"title": "STitle3", "_key": "38165ff4-9da6-df-9a8b-a162aa7a68e8"}, {"title": "S
"," _key ":" e2adb75e-9254-4774-b735-"}, {" title ":" STitle6 "," _key ":" 381f54d0-d759-43a3-94b3 "}, {" title ":" STitle7 "," _key ":" 8253-f2b6a1d6f836 "}, {" title":" STitle8 "," _key ":" bc69692b-48d8-4bd7-b62b "}]},
{"id_name": "Item2", "informational": {"values": ["werwe", "werwwe", "8", "ewrwrw", "werewrew", "64432.5390625", "64432.55859375", "64432.36328125", "werw werwerw", "2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "fields": ["werwerw", "erwrwr", "wewrewrer", "werrwrwer", "werwerwrw", "werwerewr", "werwrwr", "stuff", "vendor_product", "version"]}, "role": ["Application Server"], "cpu_cores": ["8"], "create_time": "2017-04-03 16:32:27.738432", "mod_timestamp": "2019-06-26T01:17:23.933103+00:00", "title": "Item2", "family": ["dfsfd"], "OS": ["dfdsfsf"], "sdfdsfdsds": "fdsfdsf", "dsfdsfsd": ["64432.5390625", "64432.55859375", "64432.36328125"], "host": ["dfdsfsdfsdfds"], "sdfdsfds": "sdfdsf", "vend": ["sdada"], "permissions": {"delete": true, "write": true, "user": "dsdsds", "group": {"delete": true, "write": true, "read": true}, "read": true}, "sdsdsdsdsds": ["dfsdfdsfdsfsd"], "_version": "3", "sgrp": "default", "object_type": "dfsfs", "mod_by": "user", "mod_time": "2019-06-25 13:09:47.543535", "_user": "user", "environment": ["dfsdfdfsd"], "description": "", "identifier": {"values": ["dfsdfdfdsffdfsdfs"], "fields": ["host"]}, "sdfdsfsfds": ["SMP"], "role": ["operating_system_host"], "mod_source": "REST", "_key": "afderea-be2d-47a6-9f0d-00857ereef6c", "version": ["2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "create_source": "unknown", "services": [{"title": "STitle", "_key": "865defee-d47f-4b8f-9435-bc4ere89e9b1f8d"}, {"title": "STitle2", "_key": "d9d5e231-3841-4376-a295-ea5fere95168482"}]},
{"id_name": "Item3", "informational": {"values": ["werwe", "werwwe", "8", "ewrwrw", "werewrew", "64432.5390625", "64432.55859375", "64432.36328125", "werw werwerw", "2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "fields": ["werwerw", "erwrwr", "wewrewrer", "werrwrwer", "werwerwrw", "werwerewr", "werwrwr", "stuff", "vendor_product", "version"]}, "role": ["Application Server"], "cpu_cores": ["8"], "create_time": "2017-04-03 16:32:27.738432", "mod_timestamp": "2019-06-26T01:17:23.933103+00:00", "title": "Item3", "family": ["dfsfd"], "OS": ["dfdsfsf"], "sdfdsfdsds": "fdsfdsf", "dsfdsfsd": ["64432.5390625", "64432.55859375", "64432.36328125"], "host": ["dfdsfsdfsdfds"], "sdfdsfds": "sdfdsf", "vend": ["sdada"], "permissions": {"delete": true, "write": true, "user": "dsdsds", "group": {"delete": true, "write": true, "read": true}, "read": true}, "sdsdsdsdsds": ["dfsdfdsfdsfsd"], "_version": "3", "sgrp": "default", "object_type": "dfsfs", "mod_by": "user", "mod_time": "2019-06-25 13:09:47.543535", "_user": "user", "environment": ["dfsdfdfsd"], "description": "", "identifier": {"values": ["dfsdfdfdsffdfsdfs"], "fields": ["host"]}, "sdfdsfsfds": ["SMP"], "role": ["operating_system_host"], "mod_source": "REST", "_key": "afderea-be2d-47a6-9f0d-00857ereef6c", "version": ["2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "create_source": "unknown", "services": [{"title": "STitle", "_key": "865defee-d47f-4b8f-9435-bc4ere89e9b1f8d"}, {"title": "STitle2", "_key": "d9d5e231-3841-4376-a295-ea5fere95168482"}, {"title": "STitle3", "_key": "38165ff4-9da6-df-9a8b-a162aa7a68e8"}, {"title": "SSTitle5", "_key": "e2adb75e-9254-4774-b735-"}, {"title": "STitle6", "_key": "381f54d0-d759-43a3-94b3"}, {"title": "STitle7", "_key": "8253-f2b6a1d6f836"}, {"title": "STitle8", "_key": "bc69692b-48d8-4bd7-b62b"}]}]
EDIT: можно использовать id_name вместо заголовка
EDIT: добавлено лучшепример данных
{"id_name": "Item3", "informational": {"values": ["werwe", "werwwe", "8", "ewrwrw", "werewrew", "64432.5390625", "64432.55859375", "64432.36328125", "werw werwerw", "2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "fields": ["werwerw", "erwrwr", "wewrewrer", "werrwrwer", "werwerwrw", "werwerewr", "werwrwr", "stuff", "vendor_product", "version"]}, "role": ["Application Server"], "cpu_cores": ["8"], "create_time": "2017-04-03 16:32:27.738432", "mod_timestamp": "2019-06-26T01:17:23.933103+00:00", "title": "Item3", "family": ["dfsfd"], "OS": ["dfdsfsf"], "sdfdsfdsds": "fdsfdsf", "dsfdsfsd": ["64432.5390625", "64432.55859375", "64432.36328125"], "host": ["dfdsfsdfsdfds"], "sdfdsfds": "sdfdsf", "vend": ["sdada"], "permissions": {"delete": true, "write": true, "user": "dsdsds", "group": {"delete": true, "write": true, "read": true}, "read": true}, "sdsdsdsdsds": ["enttitle"=Item1, 'hostname=myHostname"], "_version": "3", "sgrp": "default", "object_type": "dfsfs", "mod_by": "user", "mod_time": "2019-06-25 13:09:47.543535", "_user": "user", "environment": ["dfsdfdfsd"], "description": "", "identifier": {"values": ["dfsdfdfdsffdfsdfs"], "fields": ["host"]}, "sdfdsfsfds": ["SMP"], "role": ["operating_system_host"], "mod_source": "REST", "_key": "afderea-be2d-47a6-9f0d-00857ereef6c", "version": ["2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "create_source": "unknown", "services": [{"title": "STitle", "_key": "865defee-d47f-4b8f-9435-bc4ere89e9b1f8d"}, {"title": "STitle2", "_key": "d9d5e231-3841-4376-a295-ea5fere95168482"}, {"title": "STitle3", "_key": "38165ff4-9da6-df-9a8b-a162aa7a68e8"}, {"title": "SSTitle5", "_key": "e2adb75e-9254-4774-b735-"}, {"title": "STitle6", "_key": "381f54d0-d759-43a3-94b3"}, {"title": "STitle7", "_key": "8253-f2b6a1d6f836"}, {"title": "STitle8", "_key": "bc69692b-48d8-4bd7-b62b"},{"id_name": "Item3", "informational": {"values": ["werwe", "werwwe", "8", "ewrwrw", "werewrew", "64432.5390625", "64432.55859375", "64432.36328125", "werw werwerw", "2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "fields": ["werwerw", "erwrwr", "wewrewrer", "werrwrwer", "werwerwrw", "werwerewr", "werwrwr", "stuff", "vendor_product", "version"]}, "role": ["Application Server"], "cpu_cores": ["8"], "create_time": "2017-04-03 16:32:27.738432", "mod_timestamp": "2019-06-26T01:17:23.933103+00:00", "title": "Item3", "family": ["dfsfd"], "OS": ["dfdsfsf"], "sdfdsfdsds": "fdsfdsf", "dsfdsfsd": ["64432.5390625", "64432.55859375", "64432.36328125"], "host": ["dfdsfsdfsdfds"], "sdfdsfds": "sdfdsf", "vend": ["sdada"], "permissions": {"delete": true, "write": true, "user": "dsdsds", "group": {"delete": true, "write": true, "read": true}, "read": true}, "sdsdsdsdsds": ["enttitle"=Item2, 'hostname=myHostname"], "_version": "3", "sgrp": "default", "object_type": "dfsfs", "mod_by": "user", "mod_time": "2019-06-25 13:09:47.543535", "_user": "user", "environment": ["dfsdfdfsd"], "description": "", "identifier": {"values": ["dfsdfdfdsffdfsdfs"], "fields": ["host"]}, "sdfdsfsfds": ["SMP"], "role": ["operating_system_host"], "mod_source": "REST", "_key": "afderea-be2d-47a6-9f0d-00857ereef6c", "version": ["2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "create_source": "unknown", "services": [{"title": "STitle", "_key": "865defee-d47f-4b8f-9435-bc4ere89e9b1f8d"}, {"title": "STitle2", "_key": "d9d5e231-3841-4376-a295-ea5fere95168482"}, {"title": "STitle3", "_key": "38165ff4-9da6-df-9a8b-a162aa7a68e8"}, {"title": "SSTitle4", "_key": "e2adb75e-9254-4774-b735-"}, {"title": "STitle6", "_key": "381f54d0-d759-43a3-94b3"}, {"title": "STitle7", "_key": "8253-f2b6a1d6f836"}, {"title": "STitle8", "_key": "bc69692b-48d8-4bd7-b62b"},{"id_name": "Item3", "informational": {"values": ["werwe", "werwwe", "8", "ewrwrw", "werewrew", "64432.5390625", "64432.55859375", "64432.36328125", "werw werwerw", "2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "fields": ["werwerw", "erwrwr", "wewrewrer", "werrwrwer", "werwerwrw", "werwerewr", "werwrwr", "stuff", "vendor_product", "version"]}, "role": ["Application Server"], "cpu_cores": ["8"], "create_time": "2017-04-03 16:32:27.738432", "mod_timestamp": "2019-06-26T01:17:23.933103+00:00", "title": "Item3", "family": ["dfsfd"], "OS": ["dfdsfsf"], "sdfdsfdsds": "fdsfdsf", "dsfdsfsd": ["64432.5390625", "64432.55859375", "64432.36328125"], "host": ["dfdsfsdfsdfds"], "sdfdsfds": "sdfdsf", "vend": ["sdada"], "permissions": {"delete": true, "write": true, "user": "dsdsds", "group": {"delete": true, "write": true, "read": true}, "read": true}, "sdsdsdsdsds": ["enttitle"=Item3, 'hostname=myHostname"], "_version": "3", "sgrp": "default", "object_type": "dfsfs", "mod_by": "user", "mod_time": "2019-06-25 13:09:47.543535", "_user": "user", "environment": ["dfsdfdfsd"], "description": "", "identifier": {"values": ["dfsdfdfdsffdfsdfs"], "fields": ["host"]}, "sdfdsfsfds": ["SMP"], "role": ["operating_system_host"], "mod_source": "REST", "_key": "afderea-be2d-47a6-9f0d-00857ereef6c", "version": ["2.6.32-754.6.3.el6.x86_64", "2.6.32-696.16.1.el6.x86_64", "2.6.32-696.3.1.el6.x86_64", "2.6.32-642.13.1.el6.x86_64"], "create_source": "unknown", "services": [{"title": "STitle", "_key": "865defee-d47f-4b8f-9435-bc4ere89e9b1f8d"}, {"title": "STitle2", "_key": "d9d5e231-3841-4376-a295-ea5fere95168482"}, {"title": "STitle3", "_key": "38165ff4-9da6-df-9a8b-a162aa7a68e8"}, {"title": "SSTitle5", "_key": "e2adb75e-9254-4774-b735-"}, {"title": "STitle6", "_key": "381f54d0-d759-43a3-94b3"}, {"title": "STitle7", "_key": "8253-f2b6a1d6f836"}, {"title": "STitle8", "_key": "bc69692b-48d8-4bd7-b62b"}]}]
" enttitle = " поле содержит именно то значение, которое мне нужно. Файл данных состоит из одной строки, без разрывов строк.