I want to format below json code and print only specific values.
I tried using below command and got the below json output. But when I further try to parse the code for end result it didn't worked as expected.
So, a little help can make things work for me.
Command that results in below JSON code output:
mgmt_cli -r true -d 192.168.86.201 show access-rulebase name Network --format json | jq --raw-output ' .rulebase[].rulebase[] | {name: .name, rule_number: ."rule-number"}'
JSON Code:
{
"name": "MgmtRule",
"rule_number": 1
}
{
"name": null,
"rule_number": 2
}
{
"name": null,
"rule_number": 3
}
{
"name": "Inside Rules",
"rule_number": 4
}
{
"name": null,
"rule_number": 5
}
{
"name": null,
"rule_number": 6
}
{
"name": null,
"rule_number": 7
}
{
"name": null,
"rule_number": 8
}
{
"name": null,
"rule_number": 9
}
{
"name": "Cleanup rule",
"rule_number": 10
}
Further formatting jq command that I added to the above command to get the end result:
jq --slurp '"\(.[0])-\(.[length -1])"'
Unexpected Output:
"{\"name\":\"MgmtRule\",\"rule_number\":1}-{\"name\":\"Cleanup rule\",\"rule_number\":10}"
Expected Output:
MgmtRule (1-3)
Inside Rules (4-9)
Cleanup rule (10)
Raw output:
Short form of original raw output:
{
"uid" : "38271c2f-ab44-4e25-9aa4-e219cb6e12cf",
"name" : "Network",
"rulebase" : [ {
"uid" : "38acf747-85ee-4962-a3e7-d3cd678c388f",
"name" : "Internal Rules",
"type" : "access-section",
"from" : 1,
"to" : 3,
"rulebase" : [ {
"uid" : "bdd9d868-51b2-4210-8c37-f0237cd560fa",
"name" : "Test",
"type" : "access-rule",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"rule-number" : 1,
"track" : {
"type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
"per-session" : false,
"per-connection" : true,
"accounting" : false,
"enable-firewall-session" : false,
"alert" : "none"
},
"source" : [ "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9" ],
"source-negate" : false,
"destination" : [ "375f4a15-1a7d-0b48-8ffc-2e9eb4e512ae" ],
"destination-negate" : false,
"service" : [ "97aeb443-9aea-11d5-bd16-0090272ccb30", "18ec9eaa-1657-4240-ab97-5f234623336b" ],
"service-negate" : false,
"service-resource" : "",
"vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"action" : "6c488338-8eec-4103-ad21-cd461ac2c472",
"action-settings" : {
"enable-identity-captive-portal" : false
},
"content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"content-negate" : false,
"content-direction" : "any",
"time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"custom-fields" : {
"field-1" : "",
"field-2" : "",
"field-3" : ""
},
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1674486777583,
"iso-8601" : "2023-01-23T20:42+0530"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1674486722309,
"iso-8601" : "2023-01-23T20:42+0530"
},
"creator" : "admin"
},
"comments" : "",
"enabled" : true,
"install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
"available-actions" : {
"edit" : "true",
"delete" : "true",
"clone" : "not_supported"
}
}, {
"uid" : "bb93b38a-c672-49ec-afb0-8a90e8518437",
"type" : "access-rule",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"rule-number" : 2,
"track" : {
"type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
"per-session" : false,
"per-connection" : true,
"accounting" : false,
"enable-firewall-session" : false,
"alert" : "none"
},
"source" : [ "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9" ],
"source-negate" : false,
"destination" : [ "8dbb91f3-786b-4d11-a029-45ee5b7d59cf" ],
"destination-negate" : false,
"service" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"service-negate" : false,
"service-resource" : "",
"vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"action" : "6c488338-8eec-4103-ad21-cd461ac2c472",
"action-settings" : {
"enable-identity-captive-portal" : false
},
"content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"content-negate" : false,
"content-direction" : "any",
"time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"custom-fields" : {
"field-1" : "",
"field-2" : "",
"field-3" : ""
},
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1674488952100,
"iso-8601" : "2023-01-23T21:19+0530"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1674488917458,
"iso-8601" : "2023-01-23T21:18+0530"
},
"creator" : "admin"
},
"comments" : "",
"enabled" : true,
"install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
"available-actions" : {
"edit" : "true",
"delete" : "true",
"clone" : "not_supported"
}
}, {
"uid" : "925f210e-ce4f-4139-870e-3cd53f46d832",
"type" : "access-rule",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"rule-number" : 3,
"track" : {
"type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
"per-session" : false,
"per-connection" : true,
"accounting" : false,
"enable-firewall-session" : false,
"alert" : "none"
},
"source" : [ "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9" ],
"source-negate" : false,
"destination" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"destination-negate" : false,
"service" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"service-negate" : false,
"service-resource" : "",
"vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"action" : "6c488338-8eec-4103-ad21-cd461ac2c473",
"action-settings" : { },
"content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"content-negate" : false,
"content-direction" : "any",
"time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"custom-fields" : {
"field-1" : "",
"field-2" : "",
"field-3" : ""
},
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1674490197006,
"iso-8601" : "2023-01-23T21:39+0530"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1674490169763,
"iso-8601" : "2023-01-23T21:39+0530"
},
"creator" : "admin"
},
"comments" : "",
"enabled" : true,
"install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
"available-actions" : {
"edit" : "true",
"delete" : "true",
"clone" : "not_supported"
}
} ]
}, {
"uid" : "da0fdbf6-360b-44a4-94a8-b01894d3b5a0",
"name" : "cleanup rule",
"type" : "access-section",
"from" : 4,
"to" : 4,
"rulebase" : [ {
"uid" : "2b922948-da96-4c9d-a654-063e0183f9ae",
"name" : "Cleanup rule",
"type" : "access-rule",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"rule-number" : 4,
"track" : {
"type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
"per-session" : false,
"per-connection" : true,
"accounting" : false,
"enable-firewall-session" : false,
"alert" : "none"
},
"source" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"source-negate" : false,
"destination" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"destination-negate" : false,
"service" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"service-negate" : false,
"service-resource" : "",
"vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"action" : "6c488338-8eec-4103-ad21-cd461ac2c473",
"action-settings" : { },
"content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"content-negate" : false,
"content-direction" : "any",
"time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"custom-fields" : {
"field-1" : "",
"field-2" : "",
"field-3" : ""
},
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1674486751969,
"iso-8601" : "2023-01-23T20:42+0530"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1668629634218,
"iso-8601" : "2022-11-17T01:43+0530"
},
"creator" : "System"
},
"comments" : "",
"enabled" : true,
"install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
"available-actions" : {
"edit" : "true",
"delete" : "true",
"clone" : "not_supported"
}
} ]
} ],
"objects-dictionary" : [ {
"uid" : "6c488338-8eec-4103-ad21-cd461ac2c472",
"name" : "Accept",
"type" : "RulebaseAction",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"icon" : "Actions/actionsAccept",
"color" : "none"
}, {
"uid" : "8dbb91f3-786b-4d11-a029-45ee5b7d59cf",
"name" : "All_Internet",
"type" : "address-range",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"ipv4-address-first" : "0.0.0.0",
"ipv4-address-last" : "255.255.255.255",
"icon" : "Objects/ip",
"color" : "sienna"
}, {
"uid" : "97aeb369-9aea-11d5-bd16-0090272ccb30",
"name" : "Any",
"type" : "CpmiAnyObject",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"icon" : "General/globalsAny",
"color" : "black"
}, {
"uid" : "6c488338-8eec-4103-ad21-cd461ac2c473",
"name" : "Drop",
"type" : "RulebaseAction",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"icon" : "Actions/actionsDrop",
"color" : "none"
}, {
"uid" : "375f4a15-1a7d-0b48-8ffc-2e9eb4e512ae",
"name" : "dummy",
"type" : "simple-gateway",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"icon" : "NetworkObjects/management",
"color" : "black"
}, {
"uid" : "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9",
"name" : "H_192.168.86.111",
"type" : "host",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"ipv4-address" : "192.168.86.111",
"icon" : "Objects/host",
"color" : "black"
}, {
"uid" : "97aeb443-9aea-11d5-bd16-0090272ccb30",
"name" : "https",
"type" : "service-tcp",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"port" : "443",
"icon" : "Protocols/HTTP",
"color" : "red"
}, {
"uid" : "598ead32-aa42-4615-90ed-f51a5928d41d",
"name" : "Log",
"type" : "Track",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"icon" : "Track/tracksLog",
"color" : "none"
}, {
"uid" : "6c488338-8eec-4103-ad21-cd461ac2c476",
"name" : "Policy Targets",
"type" : "Global",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"icon" : "General/globalsAny",
"color" : "none"
}, {
"uid" : "18ec9eaa-1657-4240-ab97-5f234623336b",
"name" : "ssh",
"type" : "service-tcp",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"port" : "22",
"icon" : "Services/TCPService",
"color" : "red"
} ],
"from" : 1,
"to" : 4,
"total" : 4
}
Elements that I'm trying to fetch and get the end result from above raw output:
"name" & "rule-number"
Expected Output from above raw data:
Internal Rules (1-3)
cleanup rule (4)
By transforming the unprocessed, raw output generated by mgmt_cli, the solution becomes almost trivial since the JSON already contains the "to" and "from" properties.
Input (shortened to the relevant bits):
{
"uid": "38271c2f-ab44-4e25-9aa4-e219cb6e12cf",
"name": "Network",
"rulebase": [
{
"uid": "38acf747-85ee-4962-a3e7-d3cd678c388f",
"name": "Internal Rules",
"type": "access-section",
"from": 1,
"to": 3
},
{
"uid": "da0fdbf6-360b-44a4-94a8-b01894d3b5a0",
"name": "cleanup rule",
"type": "access-section",
"from": 4,
"to": 4
}
],
"from": 1,
"to": 4,
"total": 4
}
Jq transformation:
... | jq -r '.rulebase[]
| if .from == .to then "\(.name) (\(.from))"
else "\(.name) (\(.from)-\(.to))"
end'
Output:
Internal Rules (1-3)
cleanup rule (4)
Or perhaps (but I find the if/then more straightforward):
.rulebase[]
| (select(.from == .to) | "\(.name) (\(.from)-\(.to))")
// "\(.name) (\(.from)"
This question already has answers here:
Updating a Nested Array with MongoDB
(2 answers)
Closed 4 years ago.
I am attempting to update my mongo collection with the following structure
{
"_id" : "T6GqWsi9qSxnyGzgC",
"spec_name" : "test",
"version" : "test",
"message_type" : "test",
"message_trigger" : "test",
"segments" : [
{
"segment_id" : "MSH",
"sequences" : [
{
"dataType" : "ST",
"optionality" : "R",
"name" : "Field Seperator"
},
{
"dataType" : "ST",
"optionality" : "R",
"name" : "Encoding Characters"
},
{
"dataType" : "HD",
"optionality" : "O",
"name" : "Sending Application"
},
{
"dataType" : "HD",
"optionality" : "O",
"name" : "Sending Facility"
},
{
"dataType" : "HD",
"optionality" : "O",
"name" : "Receiving Application"
},
{
"dataType" : "HD",
"optionality" : "O",
"name" : "Receiving Facility"
},
{
"dataType" : "TS",
"optionality" : "O",
"name" : "Data/Time Of Message"
},
{
"dataType" : "ST",
"optionality" : "O",
"name" : "Security"
},
{
"dataType" : "CM",
"optionality" : "R",
"name" : "Message Type"
},
{
"dataType" : "ST",
"optionality" : "R",
"name" : "Message Control ID"
},
{
"dataType" : "PT",
"optionality" : "R",
"name" : "Processing ID"
},
{
"dataType" : "NM",
"optionality" : "O",
"name" : "Sequence ID"
},
{
"dataType" : "ST",
"optionality" : "O",
"name" : "Continuation Pointer"
},
{
"dataType" : "ID",
"optionality" : "O",
"name" : "Accept Acknowledgment Type"
},
{
"dataType" : "ID",
"optionality" : "O",
"name" : "Application Acknowledgment Type"
},
{
"dataType" : "ID",
"optionality" : "O",
"name" : "Country Code"
},
{
"dataType" : "ID",
"optionality" : "O",
"name" : "Character Set"
},
{
"dataType" : "CE",
"optionality" : "O",
"name" : "Principal Language Of Message"
}
]
}
],
"segment_groups" : [
{
"grouping_id" : 692335,
"segments" : [
{
"segment_id" : "test",
"group_segment_id" : 597268,
"sequences" : [ ]
}
]
}
]
}
I wish to append { value_one: "value", value_two: "value" } into the sequences array within segment_groups. My query looks like this:
db.messages.update({"segment_groups.segments.group_segment_id": 597268},{$push:{ "segment_groups.segments.$.sequences":{"value_one":"value", "value_two":"value"}}})
when attempting this i get the error:
"code" : 16837,
"errmsg" : "cannot use the part (segment_groups of segment_groups.segments.0.sequences) to traverse the element ({segment_groups: [ { grouping_id: 692335, segments: [ { segment_id: \"test\", group_segment_id: 597268, sequences: [] } ] } ]})"
enter code here
Ive been at this issue for a day now and have no come across any sound advice on how to resolve or how to restructure my collection. I am new to mongo and am not aware of best practices when structuring so any method needed to make this a better solution is valuable.
The problem you have is that your model has two levels of nested arrays (segments_groups and segments) and Mongo is not able to figure out which items in that arrays needs to be updated.
In MongoDB 3.6 it's easy to fix that using arrayFilters / positional filtered operator. In your update path you specify placeholders that represent one particular item for each array and then you have to add matching conditions as arrayFilters to help MongoDB finding which item you want to update. In your case:
db.messages.update({"_id" : "T6GqWsi9qSxnyGzgC"},
{ $push: { "segment_groups.$[sg].segments.$[s].sequences": {"value_one":"value", "value_two":"value"} } },
{ arrayFilters: [ { "sg.grouping_id": 692335 }, { "s.group_segment_id": 597268 } ] })
So $[sg] is one particular segment_group and $[s] represents one segment.
I'm new to Elasticsearch and I have the following question about parameter comparison at filter/query:
In SQL I have something like the code below, where I compare a SQL value with a parameter value and the parameter value with a fixed value
WHERE (customer.id = :customer_id OR :customer_id = '111')
The left part of the OR above is ok, but how I compare the right part?
{ "bool": {
"should": [
{ "term":
{ "customer.id": "#customer_id" }
},
{
????
}
]
}}
Thanks in advance.
UPDATE
The mapping:
{
"_id" : {
"store" : true,
"index" : "not_analyzed"
},
"_timestamp" : {
"enabled" : true,
"store" : true
},
"properties" : {
...
"processDefinition.id" : {
"type" : "string",
"index" : "not_analyzed",
"doc_values" : true
},
"processDefinition.key" : {
"type" : "string",
"index" : "not_analyzed",
"doc_values" : true
},
"processDefinition.name" : {
"type" : "string"
},
"variables" : {
"type" : "nested",
"properties" : {
...
"executionId" : {
"type" : "string",
"index" : "not_analyzed",
"doc_values" : true
},
"id" : {
"type" : "string",
"index" : "not_analyzed",
"doc_values" : true
},
"name" : {
"type" : "string",
"index" : "not_analyzed",
"doc_values" : true
},
"originalType" : {
"type" : "string",
"index" : "not_analyzed",
"doc_values" : true
},
"rawValue" : {
"type" : "string",
"index" : "not_analyzed",
"doc_values" : true
},
"storedType" : {
"type" : "string",
"index" : "not_analyzed",
"doc_values" : true
},
"stringValue" : {
"type" : "string"
}
}
}
}
}
I want every "variables.rawValue": "#parameter" OR "#parameter": "ABC". Keep in mind that if #parameter is equals to "a", then the filter returns only info related to "a". If #parameter is equals to "ABC", then the filter returns info related to everything, because company ABC is owner of everything sub company (like "a). I can have a user from "a" using the filter or a user from "ABC".
I tried this:
"bool": {
"should": {
"terms": {
"variables.rawValue": [
"#currentUser_company",
"ABC"
]
}
}
}
But the filter join the result from "a" with "ABC".
One last thing. The ES version is 1.7.
Have you tried this:
{
"query": {
"bool": {
"should": [
{
"terms": {
"FIELD": [
"#customer_id",
"111"
]
}
}
]
}
}
}
#customer_id is the value which user passes
Anyway to create a Java POJO template from a JSON string? I'm using Jersey
{
"reviewData" : [ {
"projectKey" : "CR-FOO",
"name" : "Example review.",
"description" : "Description or statement of objectives for this example review.",
"author" : {
"userName" : "joe",
"displayName" : "Joe Krustofski",
"avatarUrl" : "http://foo.com/avatar"
},
"moderator" : {
"userName" : "scott",
"displayName" : "Scott the Moderator",
"avatarUrl" : "http://foo.com/avatar"
},
"creator" : {
"userName" : "joe",
"displayName" : "Joe Krustofski",
"avatarUrl" : "http://foo.com/avatar"
},
"permaId" : {
"id" : "CR-FOO-21"
},
"permaIdHistory" : [ "CR-FOO-21" ],
"type" : "REVIEW",
"allowReviewersToJoin" : true,
"metricsVersion" : 4,
"createDate" : "2013-10-08T15:46:11.022+0200",
"dueDate" : "2013-10-09T15:46:11.022+0200",
"jiraIssueKey" : "FOO-6754"
}, {
"projectKey" : "CR-FOO",
"name" : "Example review.",
"description" : "Description or statement of objectives for this example review.",
"author" : {
"userName" : "joe",
"displayName" : "Joe Krustofski",
"avatarUrl" : "http://foo.com/avatar"
},
"moderator" : {
"userName" : "scott",
"displayName" : "Scott the Moderator",
"avatarUrl" : "http://foo.com/avatar"
},
"creator" : {
"userName" : "joe",
"displayName" : "Joe Krustofski",
"avatarUrl" : "http://foo.com/avatar"
},
"permaId" : {
"id" : "CR-FOO-21"
},
"permaIdHistory" : [ "CR-FOO-21" ],
"type" : "REVIEW",
"allowReviewersToJoin" : true,
"metricsVersion" : 4,
"createDate" : "2013-10-08T15:46:11.022+0200",
"dueDate" : "2013-10-09T15:46:11.022+0200",
"jiraIssueKey" : "FOO-6754"
} ]
}
The best solution I found was http://www.jsonschema2pojo.org/
It has support for Jackson and Maven and an online tool as well.