PowerShell Script: replace mutiple strings in a json file. Foreach methood - json

New guy in the world of power shell scripts.
I need to replace 100+ variables with new values in a BIG .json file
Following the JSON example
[
{
"_id": "631b3375030cafa9b47ebf06",
"index": 0,
"guid": "cf39106c-7afa-497a-a0bb-c4ac925f1a2a",
"isActive": true,
"balance": "$2,446.24",
"picture": "http://placehold.it/32x32",
"age": 37,
"eyeColor": "green",
"name": "Angel Jenkins",
"gender": "female",
"company": "EQUITAX",
"email": "angeljenkins#equitax.com",
"phone": "%%PHONE_NUMBER_REPLACE%%",
"address": "809 Junius Street, Bayview, Federated States Of Micronesia, 1208",
"about": "Nostrud eu enim amet irure ad deserunt aute laborum exercitation. Incididunt enim velit eiusmod quis elit deserunt ex officia irure est. Eu excepteur laboris eu nostrud officia. Cupidatat aute nulla qui ullamco eu pariatur. Culpa occaecat elit amet sit occaecat eiusmod ut ea consectetur. Sunt excepteur laboris cillum laboris. Non cillum nisi est anim ex id reprehenderit.\r\n",
"registered": "2022-01-20T10:47:40 -02:00",
"latitude": 71.06607,
"longitude": 128.932965,
"tags": [
"dolore",
"reprehenderit",
"veniam",
"in",
"do",
"reprehenderit",
"pariatur"
],
"friends": [
{
"id": 0,
"name": "%%NAME_FRIEND_TO_REPLACE%%"
},
{
"id": 1,
"name": "Angelia Holder"
},
{
"id": 2,
"name": "Dale Shelton"
}
],
"greeting": "Hello, Angel Jenkins! You have 6 unread messages.",
"favoriteFruit": "banana"
},
{
"_id": "631b33756e8ab97465edeb62",
"index": 1,
"guid": "020e9d50-6d96-485f-b019-467684f0cacb",
"isActive": true,
"balance": "$1,788.39",
"picture": "%%URL_PICTURE_REPLACE%%",
"age": 26,
"eyeColor": "brown",
"name": "Anderson Jimenez",
"gender": "male",
"company": "BRISTO",
"email": "%%%EMAIL_REPLACE%",
"phone": "+1 (866) 448-2626",
"address": "225 Euclid Avenue, Barstow, Colorado, 5335",
"about": "Aliqua exercitation sit duis qui est consequat cupidatat ea dolor aliqua laboris. Ex consectetur incididunt ea non voluptate velit. Non in deserunt commodo aute ex. Officia ex ullamco laboris labore.\r\n",
"registered": "2017-03-30T10:34:17 -03:00",
"latitude": 80.533482,
"longitude": 100.437459,
"tags": [
"qui",
"commodo",
"%%REPLACE_TAG%%",
"aute",
"et",
"duis",
"sit"
],
"friends": [
{
"id": 0,
"name": "Bowman Stephens"
},
{
"id": 1,
"name": "Tamika Phelps"
},
{
"id": 2,
"name": "Mcneil Ross"
}
],
"greeting": "Hello, Anderson Jimenez! You have 1 unread messages.",
"favoriteFruit": "banana"
},
{
"_id": "631b3375f319ce2391fdefff",
"index": 2,
"guid": "cf08598e-378f-4318-a2fa-c111ee842434",
"isActive": true,
"balance": "$1,247.75",
"picture": "http://placehold.it/32x32",
"age": 31,
"eyeColor": "blue",
"name": "Amy Murphy",
"gender": "female",
"company": "COMTENT",
"email": "amymurphy#comtent.com",
"phone": "+1 (805) 436-3728",
"address": "%%ADDRESS_REPLACE%%",
"about": "%%ABOUT_MESSAGE_TO_REPLACE%%",
"registered": "2015-02-28T01:09:54 -02:00",
"latitude": 7.595347,
"longitude": -151.936382,
"tags": [
"quis",
"anim",
"esse",
"ea",
"adipisicing",
"ea",
"est"
],
"friends": [
{
"id": 0,
"name": "Teresa Rose"
},
{
"id": 1,
"name": "Joyner Ray"
},
{
"id": 2,
"name": "Simpson Rivas"
}
],
"greeting": "Hello, Amy Murphy! You have 7 unread messages.",
"favoriteFruit": "strawberry"
},
]
I can try to do:
$filePath = ".\project\data.json"
$findString1 = "%%ABOUT_MESSAGE_TO_REPLACE%%"
$replaceString1 = "My name is Andrew"
$findString2 = "%%PHONE_NUMBER_REPLACE%%"
$replaceString2 = "0045864188"
(Get-Content $filePath) -replace $findString1, $replaceString1 -replace $findString2, $replaceString2 | Set-Content $filePath
But imagine to create 100+ variables for all the strings and replaces.
I've tried this thread, but no luck at all because I don't need to select a specific object in my json (also tried to replace, not to select). Another idea was to create a json local with key: value, iterate my json data and compare (something like this). But no luck
Any thoughts?

Put the replacements in a hashtable and repeat the -replace operation for each entry in a loop:
# read file into memory
$filePath = ".\project\data.json"
$json = Get-Content $filePath
# define replacements to be carried out
$replacements = [ordered]#{
ABOUT_MESSAGE_TO_REPLACE = "My name is Andrew"
PHONE_NUMBER_REPLACE = "0045864188"
# ... and so on
}
# replace!
foreach($label in $replacements.psbase.Keys){
$pattern = [regex]::Escape("%%${label}%%")
$substitute = $replacements[$label]
$json = $json -replace $pattern,$substitute
}
# write to disk
$json |Set-Content .\project\output.json

Related

Correct approach to return API JSON response?

I'm building a backend API for a social network project.
There's a post, which includes likes and comments, comments include likes and replies and all of them belong to a user.
My question is, when fetching a post, do i include everything else such as:
{
"id": 1,
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin faucibus, lectus vel mollis iaculis, nisl lacus faucibus mi, sit amet commodo nibh odio id tortor.",
"media": null,
"comments": [
{
"id": 1,
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"user": {
"id": 1,
"firstName": "john",
"lastName": "dog",
"username": "jonnyy",
"email": "newFreshEmail#email.com",
"country": {
"id": "MA",
"name": "Morocco"
},
"pictureFilePath": null,
"birthDate": "1600-05-01T00:00:00.000+00:00",
"creationDate": "2022-11-19T23:00:00Z"
},
"replies": [],
"likes": [],
"creationDate": "2022-11-18T23:00:00Z"
},
{
"id": 2,
"content": "Non enim praesent elementum facilisis leo.",
"user": {
"id": 2,
"firstName": "Alice",
"lastName": "cat",
"username": "ALICEEE",
"email": "newFreshEmail#email.com",
"country": {
"id": "AL",
"name": "Albania"
},
"pictureFilePath": null,
"birthDate": "1600-05-01T00:00:00.000+00:00",
"creationDate": "2022-11-20T22:43:56Z"
},
"replies": [],
"likes": [],
"creationDate": "2022-11-17T23:00:00Z"
}
],,
"likes": [
{
"id": 1,
"user": {
"id": 1,
"firstName": "john",
"lastName": "dog",
"username": "jonnyy",
"email": "newFreshEmail#email.com",
"country": {
"id": "MA",
"name": "Morocco"
},
"pictureFilePath": null,
"birthDate": "1600-05-01T00:00:00.000+00:00",
"creationDate": "2022-11-19T23:00:00Z"
},
"creationDate": "2022-11-19T23:00:00Z"
},
{
"id": 2,
"user": {
"id": 2,
"firstName": "Alice",
"lastName": "cat",
"username": "ALICEEE",
"email": "newFreshEmail#email.com",
"country": {
"id": "AL",
"name": "Albania"
},
"pictureFilePath": null,
"birthDate": "1600-05-01T00:00:00.000+00:00",
"creationDate": "2022-11-20T22:43:56Z"
},
"creationDate": "2022-11-18T23:00:00Z"
}
],
"creationDate": "2022-11-19T23:00:00Z"
}
Or keep it simple and just return something like so:
{
"id": 1,
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin faucibus, lectus vel mollis iaculis, nisl lacus faucibus mi, sit amet commodo nibh odio id tortor.",
"media": null,
"comments": [
{
"id": 1,
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"replies": [],
"likes": [],
"creationDate": "2022-11-18T23:00:00Z"
},
{
"id": 2,
"content": "Non enim praesent elementum facilisis leo.",
"replies": [],
"likes": [],
"creationDate": "2022-11-17T23:00:00Z"
}
],,
"likes": [
{
"id": 1,
"creationDate": "2022-11-19T23:00:00Z"
},
{
"id": 2,
"creationDate": "2022-11-18T23:00:00Z"
}
],
"creationDate": "2022-11-19T23:00:00Z"
}
Then fetch the user based on a particular id (be it post's, like's etc...) when needed?

How to create presets(theme styles) for shopify theme?

I'm creating a custom theme by using dawn as reference theme. I want to create 2 presets which changes all the default colors of my header, footer, announcement bar, mobile navbar, sections and the general settings.
Now i managed to change the colors of the general settings by setting the preset name and general setting variables in settings_data.json but can't seem to figure out how to change section colors. Tried alot of stuff there but it didn't work. Here's my code
{
"current": "Beige",
"presets": {
"Beige": {
"placeholder_color": "#F8E6DA",
"pagination_tab_bg_color": "#FFF8F2",
"pagination_number_color": "#000000",
"pagination_current_page_color": "#F8E6DA",
"pagination_arrow_tab_color": "#F8E6DA",
"button_hover": "#EFF6FF",
"customer_pages_bg_colors": "#ffffff",
"container_bg_color": "#FFF8F2",
"border_color": "#F8E6DA",
"primary_button_color": "#000000",
"predictive_search_bg_color": "#F8E6DA",
"predictive_search_button_color": "#FABF9D",
"predictive_search_button_text_color": "#000000",
"cart_notify_bg_color": "#FFF8F2",
"cart_notify_border_color": "#F8E6DA",
"cart_notify_primary_button_color": "#FABF9D",
"cart_notify_primary_button_text_color": "#000000",
"cart_notify_secondary_button_color": "#000000",
"cart_count_bubble_color": "#303036",
"gift_card_bg_color": "linear-gradient(#FFF1E8, #EFD1B8)",
"gift_card_badge_color": "#F8E6DA",
"gift_card_badge_text_color": "#000000",
"gift_card_primary_button_color": "#fabf9d",
"gift_card_secondary_button_color": "#000000",
"sections": {
"announcement-bar": {
"type": "announcement-bar",
"blocks": {
"announcement-bar-0": {
"type": "announcement",
"settings": {
"show_announce_home_page": false,
"text": "Welcome to our store",
"announce_font_size": 15,
"announce_text_align": "center",
"announce_bold": false,
"link": "",
"announce_text_color": "#E22120",
"announce_bg_color": "#ffffff"
}
}
},
"block_order": [
"announcement-bar-0"
],
"settings": {
}
},
"mobile-navigation": {
"type": "mobile-navigation",
"settings": {
"nav-menu": "main-menu",
"mobile_nav_text_color": "#000000",
"icon_color": "#000000",
"border_bottom": "#f8e6da",
"mobile_nav_bg_color": "#ffffff"
}
},
"header": {
"type": "header",
"settings": {
"logo_width": 100,
"menu": "main-menu",
"enable_sticky_header": true,
"show_search_box": true,
"enable_predictive_search": true,
"header_text_color": "#000000",
"icon_color": "#000000",
"header_bg_color": "#f8e6da",
"dropdown_hover_color": "#ebd3c3",
"dropdown_hover_text_color": "#000000"
}
},
"footer": {
"type": "footer",
"blocks": {
"footer-0": {
"type": "text",
"settings": {
"address": "<p>(218) 463-0260 36979 300th St<br\/>Roseau, Minnesota(MN), 56751<\/p>",
"contact": "<p>+1 (234) 567-89-90 <br\/>+1 (234) 567-89-90<\/p>",
"email": "info#collector.com"
}
},
"footer-1": {
"type": "menu",
"settings": {
"menu_heading": "Links",
"menu": "footer",
"menu_collapse": true
}
},
"88236e16-1b6c-438e-8543-f6fe494e04fb": {
"type": "menu",
"settings": {
"menu_heading": "Quick Links",
"menu": "footer-menu2",
"menu_collapse": true
}
},
"footer-2": {
"type": "info",
"settings": {
"additional_info": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc quis risus mi. Ut placerat quam lectus. Curabitur dictum velit non lacus<\/p>"
}
},
"footer-3": {
"type": "newsletter",
"settings": {
"newsletter_heading": "Subscribe to our emails",
"newsletter_btn_text": "Sign Up",
"newsletter_caption": "Phasellus dignissim, tellus in pellentesque mollis, mauris",
"newsletter_btn_color": "#fabf9d",
"newsletter_btn_text_color": "#000000"
}
}
},
"block_order": [
"footer-0",
"footer-1",
"88236e16-1b6c-438e-8543-f6fe494e04fb",
"footer-2",
"footer-3"
],
"settings": {
"footer_logo_width": 100,
"footer_top_bg_color": "#f8e6da",
"footer_top_text_color": "#000000",
"footer_bottom_bg_color": "#303036",
"footer_bottom_text_color": "#ffffff",
"show_social": true,
"payment_enable": true
}
}
},
"content_for_index": [
]
},
"Electronic": {
"placeholder_color": "#7DABE0",
"pagination_tab_bg_color": "#F8FBFF",
"pagination_current_page_color": "#EFF6FF",
"pagination_arrow_tab_color": "#EFF6FF",
"button_hover": "#EBD3C3",
"customer_pages_bg_colors": "#FCFEFF",
"container_bg_color": "#EFF6FF",
"border_color": "#134074",
"primary_button_color": "#E0691E",
"predictive_search_bg_color": "#C6D8ED",
"predictive_search_button_color": "#E0691E",
"predictive_search_button_text_color": "#ffffff",
"cart_notify_bg_color": "#EFF6FF",
"cart_notify_border_color": "#134074",
"cart_notify_primary_button_color": "#ED6F1E",
"cart_notify_primary_button_text_color": "#ffffff",
"cart_notify_secondary_button_color": "#134074",
"cart_count_bubble_color": "#ED6F1E",
"gift_card_bg_color": "linear-gradient(#EFF6FF, #7DABE0)",
"gift_card_badge_color": "#000000",
"gift_card_badge_text_color": "#ffffff",
"gift_card_primary_button_color": "#ED6F1E",
"gift_card_primary_text_button_color": "#ffffff",
"gift_card_secondary_button_color": "#134074",
"sections": {
"announcement-bar": {
"type": "announcement-bar",
"blocks": {
"announcement-bar-0": {
"type": "announcement",
"settings": {
"show_announce_home_page": false,
"text": "Welcome to our store",
"announce_font_size": 15,
"announce_text_align": "center",
"announce_bold": false,
"link": "",
"announce_text_color": "#E22120",
"announce_bg_color": "#ffffcf"
}
}
},
"block_order": [
"announcement-bar-0"
],
"settings": {
}
},
"mobile-navigation": {
"type": "mobile-navigation",
"settings": {
"nav-menu": "main-menu",
"mobile_nav_text_color": "#000000",
"icon_color": "#000000",
"border_bottom": "#f8e6da",
"mobile_nav_bg_color": "#fff7f2"
}
},
"header": {
"type": "header",
"settings": {
"logo_width": 100,
"menu": "main-menu",
"enable_sticky_header": true,
"show_search_box": true,
"enable_predictive_search": true,
"header_text_color": "#000000",
"icon_color": "#000000",
"header_bg_color": "#f8e6da",
"dropdown_hover_color": "#ebd3c3",
"dropdown_hover_text_color": "#000000"
}
},
"footer": {
"type": "footer",
"blocks": {
"footer-0": {
"type": "text",
"settings": {
"address": "<p>(218) 463-0260 36979 300th St<br\/>Roseau, Minnesota(MN), 56751<\/p>",
"contact": "<p>+1 (234) 567-89-90 <br\/>+1 (234) 567-89-90<\/p>",
"email": "info#collector.com"
}
},
"footer-1": {
"type": "menu",
"settings": {
"menu_heading": "Links",
"menu": "footer",
"menu_collapse": true
}
},
"88236e16-1b6c-438e-8543-f6fe494e04fb": {
"type": "menu",
"settings": {
"menu_heading": "Quick Links",
"menu": "footer-menu2",
"menu_collapse": true
}
},
"footer-2": {
"type": "info",
"settings": {
"additional_info": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc quis risus mi. Ut placerat quam lectus. Curabitur dictum velit non lacus<\/p>"
}
},
"footer-3": {
"type": "newsletter",
"settings": {
"newsletter_heading": "Subscribe to our emails",
"newsletter_btn_text": "Sign Up",
"newsletter_caption": "Phasellus dignissim, tellus in pellentesque mollis, mauris",
"newsletter_btn_color": "#fabf9d",
"newsletter_btn_text_color": "#000000"
}
}
},
"block_order": [
"footer-0",
"footer-1",
"88236e16-1b6c-438e-8543-f6fe494e04fb",
"footer-2",
"footer-3"
],
"settings": {
"footer_logo_width": 100,
"footer_top_bg_color": "#f8e6da",
"footer_top_text_color": "#000000",
"footer_bottom_bg_color": "#303036",
"footer_bottom_text_color": "#ffffff",
"show_social": true,
"payment_enable": true
}
}
},
"content_for_index": [
]
}
}
}
The general settings variables work with the change in style but others like announcement bar, mobile-nav, header, footer doesn't work.. my index file is in json format.
Is anything here we can do to fix this problem?
Unfortunately, Shopify presets are just general settings presets, if, for example, you have colors in your sections the only way to create two presets is two create two different sections presets, this solution creates two sections in the section selector in the theme editor for the same section. That's why it's not recommended to add colors to sections and use color schemas instead. (just like in Dawn)

JQ: Filtering a massive json with nested arrays efficiently

I have a MASSIVE json file with lots of extraneous fields that I need to trim down to only include certain fields. I've made some headway on processing the file with jq but I've run into some difficulty when it comes to pulling information out of the nested arrays in the json file, and none of the solutions I've found have seemed to work for me.
My json data looks like this:
{
"results": [
{
"url": "https://someresult.com",
"id": 5192740,
"external_id": null,
"via": {
"channel": "web",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2022-04-29T15:19:37Z",
"updated_at": "2022-04-29T15:19:38Z",
"type": null,
"subject": "My subject line",
"raw_subject": "My subject line ",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris et nulla ut sapien ultrices tempus.",
"priority": "normal",
"status": "new",
"recipient": "somebody#email.com",
"requester_id": 1234567891,
"submitter_id": 1234567891,
"assignee_id": null,
"organization_id": null,
"group_id": 123456789,
"collaborator_ids": [],
"follower_ids": [],
"email_cc_ids": [],
"forum_topic_id": null,
"problem_id": null,
"has_incidents": false,
"is_public": true,
"due_at": null,
"tags": [
"_tag_1",
"_tag_2",
"_tag_3"
],
"custom_fields": [
{
"id": 1500010396161,
"value": null
},
{
"id": 360009431333,
"value": "Keep this data"
},
{
"id": 360054304553,
"value": false
},
{
"id": 1900000317745,
"value": null
},
{
"id": 360002223154,
"value": null
},
{
"id": 360009431353,
"value": "Keep this too"
},
{
"id": 1500001920482,
"value": "Keep this data, as well!"
}
],
"followup_ids": [],
"ticket_form_id": 12345678912,
"brand_id": 112358,
"allow_channelback": false,
"allow_attachments": true,
"result_type": "ticket"
},
{
"url": "https://anotherresult.com",
"id": 5192741,
"external_id": null,
"via": {
"channel": "web",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2022-04-18T15:19:37Z",
"updated_at": "2022-04-18T15:19:38Z",
"type": null,
"subject": "My other subject line",
"raw_subject": "My other subject line ",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris et nulla ut sapien ultrices tempus.",
"priority": "normal",
"status": "new",
"recipient": "somebody#email.com",
"requester_id": 1234567892,
"submitter_id": 1234567892,
"assignee_id": null,
"organization_id": null,
"group_id": 123456780,
"collaborator_ids": [],
"follower_ids": [],
"email_cc_ids": [],
"forum_topic_id": null,
"problem_id": null,
"has_incidents": false,
"is_public": true,
"due_at": null,
"tags": [
"_tag_1",
"_tag_2",
"_tag_3"
],
"custom_fields": [
{
"id": 1500010396161,
"value": null
},
{
"id": 360009431333,
"value": "Keep this data"
},
{
"id": 360054304553,
"value": false
},
{
"id": 1900000317745,
"value": null
},
{
"id": 360002223154,
"value": null
},
{
"id": 360009431353,
"value": "Keep this too"
},
{
"id": 1500001920482,
"value": "Keep this data, as well!"
}
],
"followup_ids": [],
"ticket_form_id": 12345678913,
"brand_id": 112359,
"allow_channelback": false,
"allow_attachments": true,
"result_type": "ticket"
}
],
"facets": null,
"meta": {
"has_more": true,
"after_cursor": "eyJmaWVsZCI6ImNyZWF0ZWRfYXQiLCJkZXNjIjp0cnVlLCJ0aWVCcmVha0ZpZWxkIjoiaWQiLCJ0aWVCcmVha0Rlc2MiOmZhbHNlLCJzb3J0VmFsdWVzIjpbMTY0NjQxNTc3MjAwMCwxNTA5NDY0NjMzNTYyXSwiZXhwb3J0ZWRUaHVzRmFyIjoxMDAwLCJzZXNzaW9uU3RhcnQiOjE2NTE1MTA1MDE3MDksImNyZWF0ZWRBdCI6MTY1MTUxMDUwMTgxNywic2FsdGVkUmVxdWVzdEhhc2giOjEwMTMwNTk0MjMsInNhbHRlZEN1cnNvckhhc2giOi0xMTE3Mzc0MjIxfQ==",
"before_cursor": null
},
"links": {
"prev": null,
"next": "https://myendpoint.site.com/api/v2/search/export.json?filter%5Btype%5D=ticket&page%5Bafter%5D=eyJmaWVsZCI6ImNyZWF0ZWRfYXQiLCJkZXNjIjp0cnVlLCJ0aWVCcmVha0ZpZWxkIjoiaWQiLCJ0aWVCcmVha0Rlc2MiOmZhbHNlLCJzb3J0VmFsdWVzIjpbMTY0NjQxNTc3MjAwMCwxNTA5NDY0NjMzNTYyXSwiZXhwb3J0ZWRUaHVzRmFyIjoxMDAwLCJzZXNzaW9uU3RhcnQiOjE2NTE1MTA1MDE3MDksImNyZWF0ZWRBdCI6MTY1MTUxMDUwMTgxNywic2FsdGVkUmVxdWVzdEhhc2giOjEwMTMwNTk0MjMsInNhbHRlZEN1cnNvckhhc2giOi0xMTE3Mzc0MjIxfQ%3D%3D&page%5Bsize%5D=1000&query=group%3A360000609273+created%3E6Months"
}
}
And I want to trim it down to this:
{
"results": [
{
"url": "https://someresult.com",
"created_at": "2022-04-29T15:19:37Z",
"subject": "My subject line",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris et nulla ut sapien ultrices tempus.",
"recipient": "somebody#email.com",
"tags": [
"_tag_1",
"_tag_2",
"_tag_3"
],
"os": "Keep this data",
"is_signed_in": false,
"phone_model": "Keep this too",
"channel": "Keep this data, as well!"
}
]
}
So far, I've been able to brute force it by deleting fields by typing them all out
jq 'del(.results[] | .url, .id, .external_id, .via, .updated_at, .type, .raw_subject, .priority, .status, .requester_id, .submitter_id, .assignee_id, .organization_id, .group_id, .collaborator_ids, .follower_ids, .email_cc_ids, .problem_id, .has_incidents, .is_public, .due_at, .forum_topic_id, .satisfaction_rating, .sharing_agreement_ids, .fields, .followup_ids, .ticket_form_id, .allow_channelback, .allow_attachments, .result_type)'
but that feels absurd (still, it works).
But when I try to delete or filter for the fields I want from the custom_fields array, I get stuck (Cannot index array with string "custom fields").
My question is twofold:
Is there a cleaner way I could be selecting which fields to keep rather than specifying which to delete?
How do I grab the fields I need from the nested array, and flattening them to the same level as the rest of the fields, while also renaming them?
You can use a mapping to keep track of the custom fields wanted :
!/usr/bin/env bash
jq '{"os" : 360009431333,
"is_signed_in": 360054304553,
"phone_model" : 360009431353,
"channel" : 1500001920482} as $mapping |
.results |= map(
{ url,created_at,subject,description,recipient,tags,
} +
(.custom_fields as $custom_fields |
$mapping |
with_entries(
.value |= (. as $id | $custom_fields[]?|select(.id==$id).value)
)
)
)' input.json
The following illustrates one approach to answering the twofold question:
.results |= map(
(.custom_fields | map(.value | select(. != null))) as $values
| {url, created_at, subject, description, recipient, tags,
os: $values[0],
is_signed_in: $values[1],
phone_model: $values[2],
channel: $values[3] } )
This however results in .results having length 2.

Azure Logic App HTTP Listen Not updating values

The values never update, when i send the http requests from postman. I am using fake your json-schema to go from the schema and create the object but i cannot get the logic app trigger to accept the values. The logic app http listener is working and i get a good request but it doesnt listen :(
{
"properties": {
"Email": {
"type": "string",
"value": ""
},
"EmployeeId": {
"type": "string",
"value": ""
},
"FirstName": {
"type": "string",
"value": ""
},
"JobCode": {
"type": "string",
"value": ""
},
"LastName": {
"type": "string",
"value": ""
},
"Ledger": {
"type": "string",
"value": ""
},
"NTID": {
"type": "string",
"value": ""
},
"Title": {
"type": "string",
"value": ""
}
},
"required": [
"Email",
"EmployeeId",
"FirstName",
"LastName",
"Ledger",
"NTID",
"Title"
],
"type": "object"
}
is the schema and below is the object i am sending{
"Email": "in",
"EmployeeId": "exercitation qui nulla anim Duis",
"FirstName": "reprehenderit magna",
"LastName": "dolore",
"Ledger": "dolore occaecat deserunt",
"NTID": "id nisi exercitation ut",
"Title": "laboris veniam in voluptate",
"JobCode": "laboris voluptate mollit"
}
all the values on the http input are ""
Did you set the "Content-Type"-header to "application/json" on your request?

Remove comma from between JSON events

I was hoping to get Splunk to break up the content of the pages into events, but it's unable to. I'm trying to sed the comma that's in between the events out, but it's not going well. This is a portion of the json coming in
"last_updated":"2017-02-28T17:56:19Z"},{"id":588699,"name":null,...
and this is the sed line I'm trying
sed -e "s/},{/}+{/" -e "s/}[^}]*$/}/" secunia.txt | tr "+" "\n"
I've put it outside my for loop in the script that barmar helped with, but it's not pulling out the ,. What am I missing?
Here is some of the data:
{"id":588699,"name":null,"status":{"id":2963,"name":"Handled"},"priority":{"id":2873,"name":"Urgent"},"queue":{"id":2144,"name":"Default"},"description":null,"assigned_to":{"id":4120,"username":"user4#company.com"},"asset_list":{"id":4777,"name":"Info Security Threat_Splunk"},"advisory":{"id":199003,"advisory_identifier":"SA74447","title":"Blue Coat Security Analytics Multiple Vulnerabilities","released":"2016-12-21T15:24:53Z","modified_date":"2016-12-21T15:24:53Z","criticality":2,"criticality_description":"Highly critical","solution_status":4,"solution_status_description":"Partial Fix","where":1,"where_description":"From remote","cvss_score":10.0,"cvss_vector":"(AV:N/AC:L/Au:N/C:C/I:C/A:C/E:U/RL:TF/RC:C)","type":0,"is_zero_day":false},"created":"2016-12-21T15:33:09Z","pretty_id":79,"custom_score":null,"last_updated":"2016-12-21T15:40:28Z"},{"id":584252,"name":null,"status":{"id":2963,"name":"Handled"},"priority":{"id":2873,"name":"Urgent"},"queue":{"id":2144,"name":"Default"},"description":null,"assigned_to":{"id":4118,"username":"user3#company.com"},"asset_list":{"id":4657,"name":"PSS Middleware Environment"},"advisory":{"id":195840,"advisory_identifier":"SA73221","title":"Oracle Solaris Multiple Third Party Components Multiple Vulnerabilities","released":"2016-10-19T14:20:02Z","modified_date":"2016-12-19T14:42:30Z","criticality":2,"criticality_description":"Highly critical","solution_status":2,"solution_status_description":"Vendor Patched","where":1,"where_description":"From remote","cvss_score":10.0,"cvss_vector":"(AV:N/AC:L/Au:N/C:C/I:C/A:C/E:U/RL:OF/RC:C)","type":0,"is_zero_day":false},"created":"2016-12-20T13:43:24Z","pretty_id":76,"custom_score":null,"last_updated":"2017-01-11T19:47:09Z"}
Try this command -
sed -e "s/,//g" -e "s/}{/}\n{/" -e "s/}[^}]*$/}/" f
Replacing the event separator works here, this assumes it does not occur elsewhere in the input though. For example:
sed 's/},{/}\n{/' secunia.txt | jq -s .
Or with portable sed:
sed 's/},{/}\
{/' secunia.txt | jq -s .
Output:
[
{
"id": 588699,
"name": null,
"status": {
"id": 2963,
"name": "Handled"
},
"priority": {
"id": 2873,
"name": "Urgent"
},
"queue": {
"id": 2144,
"name": "Default"
},
"description": null,
"assigned_to": {
"id": 4120,
"username": "user4#company.com"
},
"asset_list": {
"id": 4777,
"name": "Info Security Threat_Splunk"
},
"advisory": {
"id": 199003,
"advisory_identifier": "SA74447",
"title": "Blue Coat Security Analytics Multiple Vulnerabilities",
"released": "2016-12-21T15:24:53Z",
"modified_date": "2016-12-21T15:24:53Z",
"criticality": 2,
"criticality_description": "Highly critical",
"solution_status": 4,
"solution_status_description": "Partial Fix",
"where": 1,
"where_description": "From remote",
"cvss_score": 10,
"cvss_vector": "(AV:N/AC:L/Au:N/C:C/I:C/A:C/E:U/RL:TF/RC:C)",
"type": 0,
"is_zero_day": false
},
"created": "2016-12-21T15:33:09Z",
"pretty_id": 79,
"custom_score": null,
"last_updated": "2016-12-21T15:40:28Z"
},
{
"id": 584252,
"name": null,
"status": {
"id": 2963,
"name": "Handled"
},
"priority": {
"id": 2873,
"name": "Urgent"
},
"queue": {
"id": 2144,
"name": "Default"
},
"description": null,
"assigned_to": {
"id": 4118,
"username": "user3#company.com"
},
"asset_list": {
"id": 4657,
"name": "PSS Middleware Environment"
},
"advisory": {
"id": 195840,
"advisory_identifier": "SA73221",
"title": "Oracle Solaris Multiple Third Party Components Multiple Vulnerabilities",
"released": "2016-10-19T14:20:02Z",
"modified_date": "2016-12-19T14:42:30Z",
"criticality": 2,
"criticality_description": "Highly critical",
"solution_status": 2,
"solution_status_description": "Vendor Patched",
"where": 1,
"where_description": "From remote",
"cvss_score": 10,
"cvss_vector": "(AV:N/AC:L/Au:N/C:C/I:C/A:C/E:U/RL:OF/RC:C)",
"type": 0,
"is_zero_day": false
},
"created": "2016-12-20T13:43:24Z",
"pretty_id": 76,
"custom_score": null,
"last_updated": "2017-01-11T19:47:09Z"
}
]