How to insert new Object element in array in MongoDB - arrays

I have this Document so structured
Process: Object
Code: 1
Descr: "Analyze"
DocumentToCreate: Array
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
Type: Object
Code:"R3"
Desc:"CV"
Condition: Object
0: Object
KeyDoc: "is not null"
1: Object
ReasonInsertion.Code: "03"
2: Object
Raw.Id: "is not null"
3: Object
Product.FlagDelete: false
6: Object
7: Object
8: Object
In the Condition object in position 5(filtered by value of Type.Code "R3") I have to add a new element, to find the correct document I should filter the one with Process.Code = 1
How can I generate a script to perform this update?
UPDATE
This the data in the document
{
"_id": {
"$oid": "728b678c11f25b32b1f59d8b"
},
"Process": {
"Code": 1,
"Desc": "Analyze"
},
"DocumentToCreate": [
{
"Type": {
"Code": "19",
"Desc": "Receipt"
}
},
{
"Type": {
"Code": "20",
"Desc": "Invoice"
}
},
{
"Type": {
"Code": "PH",
"Desc": "Photo"
}
},
{
"Type": {
"Code": "11",
"Desc": "Technical documentation"
},
"Condition": [
{
"Key": "is not null"
},
{
"Raw.Id": "04"
},
{
"ReasonInsertion.Code": "99"
}
]
},
{
"Type": {
"Code": "13",
"Desc": "Booking quote"
},
"Condition": [
{
"Key": "is not null"
},
{
"Raw.Id": "01"
}
]
},
{
"Type": {
"Code": "R3",
"Desc": "CV"
},
"Condition": [
{
"Key": "is not null"
},
{
"ReasonInsertion.Code": "00"
},
{
"Raw.Id": "is not null"
},
{
"Product.FlagDelete": false
}
]
},
{
"Type": {
"Code": "50",
"Desc": "Products for sale"
},
"Condition": [
{
"Key": "is not null"
},
{
"People.Code": "Seller"
}
]
},
{
"Type": {
"Code": "C3",
"Desc": "General conditions of sale used"
},
"Condition": [
{
"Key": "is not null"
},
{
"Discount.Code": "11"
}
]
},
{
"Type": {
"Code": "A4",
"Desc": "Discounted goods"
},
"Condition": [
{
"Object.Product.Book.Type": "K,A"
}
]
},
{
"Type": {
"Code": "AC",
"Desc": "Annex Clauses"
},
"Condition": [
{
"Raw.Id": "01"
},
{
"Object.Position": "02"
}
]
},
{
"Type": {
"Code": "W3",
"Desc": "Goods on loan"
},
"Condition": [
{
"Key": "is not null"
},
{
"People.Code": "Rent"
}
]
},
{
"Type": {
"Code": "R6",
"Desc": "Return of Goods"
},
"Condition": [
{
"Raw.Id": "04"
},
{
"ReasonInsertion.Code": "OUT"
},
{
"Object.Product.Book.IsSelled": true
}
]
}
]
}
I have to add as mentioned above a new element into object "Condition" in:
"Type": {
"Code": "R3",
"Desc": "CV"
},
"Condition": [
{
"Key": "is not null"
},
{
"ReasonInsertion.Code": "00"
},
{
"Raw.Id": "is not null"
},
{
"Product.FlagDelete": false
}
]

Related

Removing and printing name/value pair from json using jolt

I want to remove a name/value pair from inside a json array and print it outside. I started by trying this and then expanding the whole request to be a json array. The solution mentioned above does not seem to be working.
Input :
[
{
"createdBy": "Admin",
"createdDate": "2022-09-08",
"modifiedBy": "Admin",
"attrs": [
{
"name": "Type",
"value": "Postpaid"
},
{
"name": "subscriber",
"value": "Paid"
},
{
"name": "Details",
"value": {
"createdDate": "today",
"description": "offer",
"id": null
}
}
],
"relatedInfo": [
{
"type": "Number",
"name": "000000"
},
{
"type": "Type",
"name": "Post"
}
]
},
{
"createdBy": "Admin",
"createdDate": "2022-09-08",
"modifiedBy": "Admin",
"attrs": [
{
"name": "Type",
"value": "Postpaid"
},
{
"name": "subscriber",
"value": "Paid"
},
{
"name": "Details",
"value": {
"createdDate": "today",
"description": "offer",
"id": null
}
}
],
"relatedInfo": [
{
"type": "Number",
"name": "000000"
},
{
"type": "Type",
"name": "Post"
}
]
}
]
Desired Output :
[
{
"createdBy": "Admin",
"createdDate": "2022-09-08",
"modifiedBy": "Admin",
"attrs": [
{
"name": "Type",
"value": "Postpaid"
},
{
"name": "subscriber",
"value": "Paid"
}
],
"Details": {
"createdDate": "today",
"description": "offer",
"id": null
},
"relatedInfo": [
{
"type": "Number",
"name": "000000"
},
{
"type": "Type",
"name": "Post"
}
]
},
{
"createdBy": "Admin",
"createdDate": "2022-09-08",
"modifiedBy": "Admin",
"attrs": [
{
"name": "Type",
"value": "Postpaid"
},
{
"name": "subscriber",
"value": "Paid"
}
],
"Details": {
"createdDate": "today",
"description": "offer",
"id": null
},
"relatedInfo": [
{
"type": "Number",
"name": "000000"
},
{
"type": "Type",
"name": "Post"
}
]
}
]
Current Jolt spec:
[
{
"operation": "shift",
"spec": {
"*": "[&]",
"attrs": {
"*": {
"name": {
"*": { "#2": "&4" },
"Details": {
"#(2,value)": "&1"
}
}
}
}
}
}
]
I can't seem to figure out how the jolt spec would change in case of the array
So far so good, just need to combine the attributes at a common node. To do this, I've used the identifiers [&1] and [&5] in order to reach the level of the outermost index within the tree such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": "[&1].&",
"attrs": {
"*": {
"name": {
"*": {
"#2": "[&5].&4"
},
"Details": {
"#(2,value)": "[&5].&1"
}
}
}
}
}
}
}
]

JSON-schema object array validation

I have a mission to validate such a JSON message :
{
"header": {
"action": "change_time",
"taskGuid": "someTaskGuid",
"publishDate": "2012-04-23T18:25:43.511Z"
},
"data": {
"code": "f2103839",
"conditions": [
{
"conditionsType": "A",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
},
{
"conditionsType": "B",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
},
{
"conditionsType": "C",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
}
]
}
}
I've made such a JSON-schema to achieve that :
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "Some schema",
"description": "Some schema",
"type": "object",
"required": [
"header",
"data"
],
"properties": {
"header": {
"type": "object",
"required": [
"action",
"taskGuid",
"publishDate"
],
"properties": {
"action": {
"enum": [
"create_work_order",
"change_time",
"cancel_work"
]
},
"taskGuid": {
"type": "string"
},
"publishDate": {
"type": "string",
"format": "date-time"
}
}
},
"data": {
"type": "object",
"required": [
"code",
"conditions"
],
"properties": {
"code": {
"type": "string"
},
"conditions": {
"type": "array",
"items": [
{
"conditionsType": "object",
"properties": {
"type": {
"enum": [
"A",
"B",
"C"
]
},
"dateBegin": {
"type": "string",
"format": "date-time"
},
"dateEnd": {
"type": "string",
"format": "date-time"
}
},
"required": [
"conditionsType",
"dateBegin",
"dateEnd"
]
}
]
}
}
}
}
}
The conditions array will consist of 1-3 objects described by items. Each object should have a unique conditionsType.
I'm checking validation with this instrument - https://www.jsonschemavalidator.net/
The problem is that this schema does validate the message, But only the first object of array is processed as de. For instance, such a JSON is validated as well (see "conditions" object #2):
{
"header": {
"action": "change_time",
"taskGuid": "someTaskGuid",
"publishDate": "2012-04-23T18:25:43.511Z"
},
"data": {
"code": "f2103839",
"conditions": [
{
"conditionsType": "A",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
},
{
"conditionsType": 123,
"dateBegin": [1,2,3],
"dateEnd": 1
},
{
"conditionsType": "C",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
}
]
}
}
Is that actually the right direction I've chosen for this task ?
Two things. You have a typo in your items schema where you actually want to have type and not conditionsType. Secondly, if the items keyword is an array, the items of the array are validated against the schemas in this order. You want to have the items keyword as a single schema which is then applied to all items. Your corrected schema for copy-paste:
{"$schema":"http://json-schema.org/draft-07/schema","title":"Some schema","description":"Some schema","type":"object","required":["header","data"],"properties":{"header":{"type":"object","required":["action","taskGuid","publishDate"],"properties":{"action":{"enum":["create_work_order","change_time","cancel_work"]},"taskGuid":{"type":"string"},"publishDate":{"type":"string","format":"date-time"}}},"data":{"type":"object","required":["code","conditions"],"properties":{"code":{"type":"string"},"conditions":{"type":"array","items":{"type":"object","properties":{"conditionsType":{"enum":["A","B","C"]},"dateBegin":{"type":"string","format":"date-time"},"dateEnd":{"type":"string","format":"date-time"}},"required":["conditionsType","dateBegin","dateEnd"]}}}}}}

JQ: Remove json objects from array

I have this json file with hundreds of entries that I need to strip from data I do not need.
Snippet:
{
"entries": [
{
"metadata": {
"tags": [
]
},
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "9kn72w8zc6fh"
}
},
"id": "vcLKKhJ3mZNfGMvVZZi07",
"type": "Entry",
"createdAt": "2021-05-20T15:14:01.358Z",
"updatedAt": "2021-09-20T15:28:30.799Z",
"environment": {
"sys": {
"id": "production",
"type": "Link",
"linkType": "Environment"
}
},
"publishedVersion": 47,
"publishedAt": "2021-09-20T15:28:30.799Z",
"firstPublishedAt": "2021-05-25T10:26:56.722Z",
"createdBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "6F84RwUIY9cXNNXBoQemqX"
}
},
"updatedBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "6F84RwUIY9cXNNXBoQemqX"
}
},
"publishedCounter": 4,
"version": 48,
"publishedBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "6F84RwUIY9cXNNXBoQemqX"
}
},
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "page"
}
}
},
"fields": {
"title": {
"de-DE": "Startseite",
"en-US": "Home"
},
"description": {
"en-US": "foo"
},
"keywords": {
"en-US": "bar"
},
"stageModules": {
"en-US": [
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "11AfBBuNK8bx3EygAS3WTY"
}
}
]
},
"contentModules": {
"en-US": [
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "7uyuyIBsXWApHqpR7Pgkac"
}
},
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "4HILHPLjqQkP2H1hA2FeBG"
}
},
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "QuwRHL3XMSkguqrL1hUzC"
}
},
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "4ZyVef5oWhQWXK9V1lr3vz"
}
}
]
},
"layout": {
"en-US": "Wide"
}
}
}
]
}
From the entries array, I actually only need:
entries.sys.id
entries.sys.contentType.sys.id
entries.fields
I came up with:
jq \
'.entries | .[] .sys, .[] .fields | del(.createdAt, .createdBy, .environment, .firstPublishedAt, .metadata, .publishedAt, .publishedBy, .publishedCounter, .publishedVersion, .space, .type, .updatedAt, .updatedBy, .version)' \
$infile >| $outfile
However, this changes the structure of the document. The entries node is missing (due to the .entries filter):
{
"id": "vcLKKhJ3mZNfGMvVZZi07",
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "page"
}
}
}
{
"id": "1UgOmHIvsWrFEf1VCa84kz",
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "moduleText"
}
}
}
{
"title": {
"de-DE": "Startseite",
"en-US": "Home"
},
"description": {
"en-US": "Foo"
},
"keywords": {
"en-US": "Bar"
},
"stageModules": {
"en-US": [
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "11AfBBuNK8bx3EygAS3WTY"
}
}
]
},
"contentModules": {
"en-US": [
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "7uyuyIBsXWApHqpR7Pgkac"
}
},
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "4HILHPLjqQkP2H1hA2FeBG"
}
},
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "QuwRHL3XMSkguqrL1hUzC"
}
},
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "4ZyVef5oWhQWXK9V1lr3vz"
}
}
]
},
"layout": {
"en-US": "Wide"
}
}
I have 2 questions:
How can I delete deeper objects, eg. .entries.sys.space.sys.linkType?
How can I keep the .entries node in the outfile?
Thank you for your help.
If you want full control over the output, I'd just re-create the desired format.
It sounds like you're trying to accieve the following format:
{
"entries": [
{
"sys": {
"id": ...
},
"contentType": {
"sys": {
"id": ...
}
},
"fields": ...
}
}
]
}
We can achieve this by using the following JQ selector:
.entries |= map({ "sys": { "id": .sys.id }, "contentType": { "sys": { "id": .sys.contentType.sys.id } }, fields })
Try it online!

Elastic - JSON Array nested in Array

I have to index a json to Elastic which look like the below format. My problem is that the key "variable" is array that contains json objects (I thought about "nested" datatype of Elastic) but some of those objects it's possible to contain nested json arrays inside them. (see variable CUSTOMERS).
POST /example_data/data {
"process_name": "TEST_PROCESS",
"process_version ": 0,
"process_id": "1111",
"activity_id": "111",
"name": "update_data",
"username": "testUser",
"datetime": "2018-01-01 10:00:00",
"variables": [{
"name": "ΒΑΝΚ",
"data_type": "STRING",
"value": "EUROBANK"
},{
"name": "CITY",
"data_type": "STRING",
"value": "LONDON"
}, {
"name": "CUSTOMERS",
"data_type": "ENTITY",
"value": [{
"variables": [{
"name": "CUSTOMER_NAME",
"data_type": "STRING",
"value": "JOHN"
}, {
"name": " CUSTOMER_CITY",
"data_type": "STRING",
"value": "LONDON"
}
]
}
]
}, {
"name": "CUSTOMERS",
"data_type": "ENTITY",
"value": [{
"variables": [{
"name": "CUSTOMER_NAME",
"data_type": "STRING",
"value": "ΑΘΗΝΑ"
}, {
"name": " CUSTOMER_CITY ",
"data_type": "STRING",
"value": "LIVERPOOL"
}, {
"name": " CUSTOMER_NUMBER",
"data_type": "STRING",
"value": "1234567890"
}
]
}
]
}
] }
When I'm trying to index it I get the following error
{ "error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Can't merge a non object mapping [variables.value] with an object mapping [variables.value]"
}
],
"type": "illegal_argument_exception",
"reason": "Can't merge a non object mapping [variables.value] with an object mapping [variables.value]" }, "status": 400 }
Mapping
{ "example_data": {
"mappings": {
"data": {
"properties": {
"activity_id": {
"type": "text"
},
"name": {
"type": "text"
},
"process_name": {
"type": "text"
},
"process_version": {
"type": "integer"
}
"process_id": {
"type": "text"
},
"datetime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"username": {
"type": "text",
"analyzer": "greek"
},
"variables": {
"type": "nested",
"properties": {
"data_type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"value": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}}}
When I remove the variable CUSTOMERS that contains the array, then It works properly because there are only json objects.
Is there a way to handle that? Thanks in advance

DataTables - Load data from pre-defined JSON

I have a problem pointing dataTable to the right spot in the JSON. I receive a nested array:
{
"status": "ok",
"count": "7",
"msg ": "Operation Successful",
"data": [{
"contactHasServiceArea": true,
"issueCategories": [{
"id": "8",
"description": "Finance"
},
{
"id": "9",
"description": "Housing"
},
{
"id": "10",
"description": "International"
}
],
"cases": [{
"id": 31645,
"client_name": "Matthew",
"issue": "Assessment Completion",
"referral": null,
"opened_date": "10\/07\/2017",
"case_status": "Open"
}, {
"id": 31668,
"client_name": "Fanky ",
"issue": "Complex",
"referral": null,
"opened_date": "01\/07\/2017",
"case_status": "Open"
}]
}]
}
How do I point to the "cases" object? I'm sure this is simply, but I'm confused by the many options in the dataTables config.
I tried variations of data, dataSrc as well as data.cases or just cases, etc.
Thanks
$('#cases_table').DataTable( {
"ajax": "ajax/getCases",
"dataSrc" : "data.cases",
"data" : "cases",
"columns": [
{ "data": "client_name" },
{ "data": "issue" },
{ "data": "referral" },
{ "data": "opened_date" },
{ "data": "case_status" }
]
} );
You can configure like this:
$('#cases_table').DataTable( {
"ajax": {
"url": "ajax/getCases",
"dataSrc" : "data.cases"
},
"columns": [
{ "data": "client_name" },
{ "data": "issue" },
{ "data": "referral" },
{ "data": "opened_date" },
{ "data": "case_status" }
]
} );
datasrc points into the returns json. Remove the data option.

Resources