Angular - get data from Object Array - arrays

Good morning, can anyone advise me? I'm pretty clueless. I've already spent a few hours on it and I don't know how to solve this.
The data is fictitious and the original JSON is far more complex.
JSON
{
"main": [
[
{
"type": "dasdasdasd",
"id": 5,
"content": {
"title": "adadadsad",
"items": [
{
"date": "2012-02-02T11:23:00Z",
"id": 12,
"name": "test",
"isEnabled": false,
"isHighlited": false,
"images": {
"lists": {
"small": [
{
"id": 18,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"original": [
{
"id": 19,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"large": [
{
"id": 22,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"medium": [
{
"id": 23,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
]
}
},
"enum": "LINIE",
"url": "https://test.com"
}
]
}
}
]
],
"second": [
]
}
How can I get from main -> content -> items -> images -> url?
More precisely each images and from that url.
Thank you very much in advance for your help.

It looks like your main array has an array as 1st element. If that is correct, you take the first element of main, and then loop over that.
let jsonObj = {
"main": [
[
{
"type": "dasdasdasd",
"id": 5,
"content": {
"title": "adadadsad",
"items": [
{
"date": "2012-02-02T11:23:00Z",
"id": 12,
"name": "test",
"isEnabled": false,
"isHighlited": false,
"images": {
"lists": {
"small": [
{
"id": 18,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"original": [
{
"id": 19,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"large": [
{
"id": 22,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"medium": [
{
"id": 23,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
]
}
},
"enum": "LINIE",
"url": "https://test.com"
}
]
}
}
]
],
"second": [
]
}
let smallImg = "", originalImg = "", largeImg = "", mediumImg = "";
jsonObj.main[0].forEach(mainItem => {
mainItem.content.items.forEach(item => {
smallImg = item.images.lists.small[0].url;
originalImg = item.images.lists.original[0].url;
largeImg = item.images.lists.large[0].url;
mediumImg = item.images.lists.medium[0].url;
});
});
console.log(smallImg);
console.log(originalImg);
console.log(largeImg);
console.log(mediumImg);

Related

How to write JOLT Spec for nested arrays? newbie here

i am really newbie on Jolt and try to solve this:
input JSON :
[
{
"id": 528356,
"version": 0,
"name": "ActiveMQ",
"nameUnique": true,
"config": {
"id": 528356,
"version": 0,
"type": "JMX_METRIC_RULE_COLLECTION",
"name": "ActiveMQ",
"description": "Default Config",
"enabled": true,
"children": [
{
"id": 2638137,
"version": 0,
"jmxCollectionId": 528356,
"type": "JMX_METRIC_RULE",
"name": "ActiveMQ_Brokers",
"exclude": false,
"enabled": true,
"metricCategory": "Broker",
"instanceIdentifier": "BrokerName"
},
{
"id": 2638138,
"version": 0,
"jmxCollectionId": 528356,
"type": "JMX_METRIC_RULE",
"name": "ActiveMQ_Queues",
"exclude": false,
"enabled": true,
"metricCategory": "Queues",
"instanceIdentifier": "Destination"
},
{
"id": 2638139,
"version": 0,
"jmxCollectionId": 528356,
"type": "JMX_METRIC_RULE",
"name": "ActiveMQ_Topics",
"exclude": false,
"enabled": true,
"metricCategory": "Topics",
"instanceIdentifier": "Destination"
}
],
"applicationId": 46546
},
"type": "JMX_METRIC_RULE_COLLECTION"
},
{
"id": 528358,
"version": 0,
"name": "Coherence",
"nameUnique": true,
"config": {
"id": 528358,
"version": 0,
"type": "JMX_METRIC_RULE_COLLECTION",
"name": "Coherence",
"description": "Default Config",
"enabled": true,
"children": [
{
"id": 2638146,
"version": 0,
"jmxCollectionId": 528358,
"type": "JMX_METRIC_RULE",
"name": "Coherence_Caches",
"exclude": false,
"enabled": true,
"metricCategory": "Coherence|Cache Maps",
"instanceIdentifier": "service,name,nodeId"
},
{
"id": 2638147,
"version": 0,
"jmxCollectionId": 528358,
"type": "JMX_METRIC_RULE",
"name": "Coherence_Cluster",
"exclude": false,
"enabled": true,
"metricCategory": "Coherence|Cluster",
"instanceIdentifier": null
}
],
"applicationId": 46546
},
"type": "JMX_METRIC_RULE_COLLECTION"
}
]
Desired output :
[
{
"name": "ActiveMQ",
"children": [
{
"name": "ActiveMQ_Brokers"
},
{
"name": "ActiveMQ_Queues"
},
{
"name": "ActiveMQ_Topics"
}
]
},
{
"name": "Coherence",
"children": [
{
"name": "Coherence_Caches"
},
{
"name": "Coherence_Cluster"
}
]
}
]
at the moment, just able to get something using spec :
[
{
"operation": "shift",
"spec": {
"*": {
"config": {
"name": "[].parent",
"children": {
"*": {
"name": "[].child"
}
}
}
}
}
}
]
Current output :
[
{
"parent": "ActiveMQ"
},
{
"child": "ActiveMQ_Brokers"
},
{
"child": "ActiveMQ_Queues"
},
{
"child": "ActiveMQ_Topics"
},
{
"parent": "Coherence"
},
{
"child": "Coherence_Caches"
},
{
"child": "Coherence_Cluster"
}
]
in short, did not find a way to correctly map using keys.
thanks for help.
Need to unite the result at some levels of nodes such like in the following spec
[
{
"operation": "shift",
"spec": {
"*": {
"config": {
"name": "[&2].&", //"[&2]" stands for reaching the indexes at uppermost level, "&" copies the literal "name" as attribute's key name.
"children": {
"*": {
"name": "[&4].&2[&1].&" //"[&4]" is needed to traverse two more levels to reach the same level what the above attribute("config.name") needs, "&2" copies the literal "children" after going two levels up the tree, "[&1]" is sub-indexes of it, "&" copies the literal "name"
}
}
}
}
}
}
]

node red function add and remove object from context array

I have a for loop in the node red function that a the start of the function I get the array from the context
var alarmArray = flow.get("alarmArray")
and after I need to push object on the array I have do this but I'm not pusching on the flow context array but in the local array
alarmArray.push({
key: "Low air pressure",
value: alarm1Cip
});
flow.set("alarm1CipOld",alarm1Cip);
and after in the for loop I need to remove the object from the context but in my way I remove it from local array
for (var key in alarmArray){
node.warn(key);
msg.payload = {
"title": alarmArray[key].key,
"isActive":alarmArray[key].value
};
node.send(msg)
delete alarmArray[key]
}
how can I manage to add and remove object in the context array?
thanks
The problem here is that NodeJS is a pass by reference language. This means that there are not 2 arrays here, only 1 that has 2 handles (var alarmArray and flow.get('alarmArray'))
This means that anything pushed/deleted on the "local" copy is also getting pushed/deleted to the copy in the context.
The only way to do what you want will be to make a deep copy of the array every time you recover it from the context and then work on the copy locally.
Assuming the array only holds simple objects then the following should work:
var alarmArray = JSON.parse(JSON.stringify(flow.get('alarmArray')))
If you want to save your changes you just have to reload the mutated object in the right context, overwriting the previous value with
flow.set('alarmArray', alarmArray)
You might better to use global.set instead of flow.set
This might help you.
[
{
"id": "133329daaceb6bb3",
"type": "tab",
"label": "flow 3",
"disabled": false,
"info": "",
"env": []
},
{
"id": "c80bec9a15b703f6",
"type": "inject",
"z": "133329daaceb6bb3",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 140,
"y": 140,
"wires": [
[
"8687551e01cd2045"
]
]
},
{
"id": "8687551e01cd2045",
"type": "function",
"z": "133329daaceb6bb3",
"name": "msg_1",
"func": "global.set(\"msg_1\",\"test\");\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 280,
"y": 140,
"wires": [
[]
]
},
{
"id": "103b03c39c8d9cae",
"type": "debug",
"z": "133329daaceb6bb3",
"name": "debug 29",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 500,
"y": 300,
"wires": []
},
{
"id": "c043cba9f34ba574",
"type": "inject",
"z": "133329daaceb6bb3",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 140,
"y": 180,
"wires": [
[
"e674c706437cc0f3"
]
]
},
{
"id": "e674c706437cc0f3",
"type": "function",
"z": "133329daaceb6bb3",
"name": "msg_2 ",
"func": "global.set(\"msg_2\",\"hello\");\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 280,
"y": 180,
"wires": [
[]
]
},
{
"id": "11f8fb75d4c4e6c0",
"type": "inject",
"z": "133329daaceb6bb3",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "1",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 150,
"y": 300,
"wires": [
[
"1346e34e8c83b2dc"
]
]
},
{
"id": "1346e34e8c83b2dc",
"type": "function",
"z": "133329daaceb6bb3",
"name": "check if 2 msg set",
"func": "var msg1 = global.get(\"msg_1\");\nvar msg2 = global.get(\"msg_2\");\n\nif(msg1 && msg2){\n msg.payload=\"both message arrived!\";\n}\nelse{\n msg.payload=\"Nope. Not yet.\";\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 330,
"y": 300,
"wires": [
[
"103b03c39c8d9cae"
]
]
},
{
"id": "62d522a844321ef9",
"type": "inject",
"z": "133329daaceb6bb3",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 540,
"y": 140,
"wires": [
[
"6d109daff443dade"
]
]
},
{
"id": "6d109daff443dade",
"type": "function",
"z": "133329daaceb6bb3",
"name": "reset",
"func": "global.set(\"msg_1\",null);\nglobal.set(\"msg_2\",null);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 690,
"y": 140,
"wires": [
[]
]
},
{
"id": "4e1848c867860d5f",
"type": "comment",
"z": "133329daaceb6bb3",
"name": "Here gonna auto-run",
"info": "",
"x": 170,
"y": 260,
"wires": []
},
{
"id": "3102e176923a8fad",
"type": "comment",
"z": "133329daaceb6bb3",
"name": "Check what if both inject clicked",
"info": "",
"x": 210,
"y": 100,
"wires": []
}
]

Filter an array of obj based on another array value

I have an array of object like this :
[
{
"url": "https://recipes-gatsby-react.netlify.app/",
"stack": [
{
"id": 1,
"title": "GatsbyJs"
},
{
"id": 2,
"title": "React"
},
{
"id": 3,
"title": "GraphQl"
}
],
"id": "Project_1",
"featured": false,
"title": "Gatsby recipes app"
},
{
"url": "https://resort-react-mr.netlify.app",
"stack": [
{
"id": 4,
"title": "React"
},
{
"id": 5,
"title": "Contentful"
}
],
"id": "Project_2",
"featured": false,
"title": "react resort"
},
{
"url": "https://color-generator-react-mr.netlify.app/",
"stack": [
{
"id": 6,
"title": "React"
}
],
"id": "Project_3",
"featured": false,
"title": "color generator"
},
{
"url": "",
"stack": [
{
"id": 7,
"title": "React Native"
},
{
"id": 8,
"title": "Firebase"
}
],
"id": "Project_4",
"featured": false,
"title": "Book-Worm"
},
{
"url": "",
"stack": [
{
"id": 9,
"title": "React Native"
},
{
"id": 10,
"title": "Firebase"
},
{
"id": 11,
"title": "Stripe"
},
{
"id": 12,
"title": "Google Api"
}
],
"id": "Project_5",
"featured": false,
"title": "Delivery App"
},
{
"url": "https://cocktails-react-app-mr.netlify.app/",
"stack": [
{
"id": 13,
"title": "React"
}
],
"id": "Project_6",
"featured": false,
"title": "Cocktails App"
},
{
"url": "https://www.ideacasaitalia.it/index.php",
"stack": [
{
"id": 14,
"title": "Prestashop"
}
],
"id": "Project_7",
"featured": false,
"title": "Idea Casa Italia"
}
]
and I have another (simple) array like this one for example
["react","prestashop"]
I would like to filter the first array based on STACK key on the value of the second array, i.e. I would like to be returned all elements that have at least one element of the array (variable).
I have come to this
const filter = value => {
const newProj = relProjects.filter(obj =>
obj.stack.some(st => st.title.toLowerCase().includes(value))
)
setProjRel(newProj)
}
console.log(filter(["react","prestashop"])
however in this way I can only get the elements that contain all the elements of the array. Instead I would like to have returned all the elements that have AT LEAST 1 stack of the ones I passedThanks to who will answer me
Using Array#filter and Array#includes
Filter through the data array. With the predicate being that the stack array has at least one item that is included in the matches array
const data = [{"url":"https://recipes-gatsby-react.netlify.app/","stack":[{"id":1,"title":"GatsbyJs"},{"id":2,"title":"React"},{"id":3,"title":"GraphQl"}],"id":"Project_1","featured":false,"title":"Gatsby recipes app"},{"url":"https://resort-react-mr.netlify.app","stack":[{"id":4,"title":"React"},{"id":5,"title":"Contentful"}],"id":"Project_2","featured":false,"title":"react resort"},{"url":"https://color-generator-react-mr.netlify.app/","stack":[{"id":6,"title":"React"}],"id":"Project_3","featured":false,"title":"color generator"},{"url":"","stack":[{"id":7,"title":"React Native"},{"id":8,"title":"Firebase"}],"id":"Project_4","featured":false,"title":"Book-Worm"},{"url":"","stack":[{"id":9,"title":"React Native"},{"id":10,"title":"Firebase"},{"id":11,"title":"Stripe"},{"id":12,"title":"Google Api"}],"id":"Project_5","featured":false,"title":"Delivery App"},{"url":"https://cocktails-react-app-mr.netlify.app/","stack":[{"id":13,"title":"React"}],"id":"Project_6","featured":false,"title":"Cocktails App"},{"url":"https://www.ideacasaitalia.it/index.php","stack":[{"id":14,"title":"Prestashop"}],"id":"Project_7","featured":false,"title":"Idea Casa Italia"}];
const matches = ["react", "prestashop"];
const filtered = data.filter(({ stack }) => stack.filter(({ title }) => matches.includes(title.toLowerCase())).length > 0);
console.log(filtered);

Jq to add the element in json array conditionally and print the entire modified file

I have a json file with the below format.
I would like to add the element {"test" : "2"}in propDefs[] if the .children[].type=="environmentApprovalTask" and .children[].role.name== "GCM approver" and output that to the new file. I want the entire file with the modified content.The .children[] array may not have always 3 elements.
{
"edges": [
{
"to": "de32e562319310b7b4fe3736e22009",
"from": "99d5f0b278f08721adba7741b782d8",
"type": "SUCCESS",
"value": ""
},
{
"to": "06916609ad7fd4127815be3f075c81",
"from": "de32e562319310b7b4fe3736e22009",
"type": "SUCCESS",
"value": ""
},
{
"to": "99d5f0b278f08721adba7741b782d8",
"type": "ALWAYS",
"value": ""
}
],
"offsets": [
{
"name": "99d5f0b278f08721adba7741b782d8",
"x": -91,
"y": 100,
"h": 70,
"w": 290
},
{
"name": "06916609ad7fd4127815be3f075c81",
"x": -5,
"y": 420,
"h": 80,
"w": 120
},
{
"name": "de32e562319310b7b4fe3736e22009",
"x": 69,
"y": 240,
"h": 70,
"w": 240
}
],
"layoutMode": "manual",
"type": "graph",
"id": "d5d9c4c4-0c5f-4642-872c-ac892039eaa4",
"name": "79e8b952-dd59-4cfd-9c0c-e6c08a81d4ca",
"children": [
{
"type": "finish",
"id": "833e959c-6825-413d-afc4-7b74c0a87c3e",
"name": "06916609ad7fd4127815be3f075c81",
"children": []
},
{
"id": "e976041d-af9d-48cf-b838-735bb5efd483",
"type": "envApprovalTask",
"children": [],
"name": "de32e562319310b7b4fe3736e22009",
"roleRestrictionData": {
"contextType": "ENVIRONMENT",
"roleRestrictions": [
{
"roleId": "087175fb-5d38-42d1-b65a-2b6a6958bc21"
}
]
},
"propDefs": [{"test": "1"}],
"templateName": "ApprovalCreated",
"commentRequired": false,
"commentPrompt": "",
"role": {
"id": "087175fb-5d38-42d1-b65a-2b6a6958bc21",
"name": "Approver",
"isDeletable": true
}
},
{
"id": "513fbc6a-3c4a-4a10-9eb0-7dc893c69413",
"type": "environmentApprovalTask",
"children": [],
"name": "99d5f0b278f08721adba7741b782d8",
"roleRestrictionData": {
"contextType": "ENVIRONMENT",
"roleRestrictions": [
{
"roleId": "116b8cd5-e7e4-403d-9599-35fe25d3cba2"
}
]
},
"propDefs": [],
"templateName": "ApprovalCreated",
"commentRequired": false,
"commentPrompt": "",
"role": {
"id": "116b8cd5-e7e4-403d-9599-35fe25d3cba2",
"name": "Manager Approver",
"isDeletable": true
}
}
]
}
My final attempt with the code
cat file.json |jq '.|.children[]| select(.type=="envApprovalTask")|select(.role.name=="Approver") |.propDefs[.profDefs|length] |= .+ {"test" : "2"}'
This only produces the modified element, didnt produce the entire file as output. Please help how can i get the desired output.
TIL about complex assignments in jq. This actually works:
(.children[] | select(.type=="envApprovalTask" and .role.name=="Approver") | .propDefs) |= .+[{"test":"2"}]
The only significant difference from your version is the parenthesised left side of the assignment.

How to flatten nested arrays based on a key of its members

I have the following JSON object:
{
"name": "Womens",
"position": 1,
"url": "/collections/womens-all",
"submenus": [
{
"name": "Apparel",
"position": 1,
"url": "/collections/womens-apparel-all",
"submenus": [
{
"name": "Tees & Tanks",
"position": 1,
"url": "/collections/womens-tees",
"submenus": []
},
{
"name": "Silk & Tops",
"position": 2,
"url": "/collections/womens-tops",
"submenus": []
},
{
"name": "Sweaters & Sweatshirts",
"position": 3,
"url": "/collections/womens-sweaters",
"submenus": []
}
]
},
{
"name": "Accessories",
"position": 2,
"url": "/collections/womens-all-accessories",
"submenus": [
{
"name": "Petra",
"position": 1,
"url": "/collections/petra",
"submenus": []
},
{
"name": "Weekenders",
"position": 2,
"url": "/collections/womens-bags",
"submenus": []
},
{
"name": "Backpacks & Totes",
"position": 3,
"url": "/collections/womens-backpacks",
"submenus": []
},
{
"name": "Accessories & Shoes",
"position": 4,
"url": "/collections/womens-accessories",
"submenus": []
}
]
}
]
}
I want to flatten this object's submenus into a single array containing every menu in the tree. The result would look like:
[
{
"name": "Apparel",
"position": 1,
"url": "/collections/womens-apparel-all",
"submenus": []
},
{
"name": "Accessories",
"position": 2,
"url": "/collections/womens-all-accessories",
"submenus": []
},
{
"name": "Petra",
"position": 1,
"url": "/collections/petra",
"submenus": []
},
{
"name": "Weekenders",
"position": 2,
"url": "/collections/womens-bags",
"submenus": []
},
{
"name": "Backpacks & Totes",
"position": 3,
"url": "/collections/womens-backpacks",
"submenus": []
},
{
"name": "Accessories & Shoes",
"position": 4,
"url": "/collections/womens-accessories",
"submenus": []
},
{
"name": "Tees & Tanks",
"position": 1,
"url": "/collections/womens-tees",
"submenus": []
},
{
"name": "Silk & Tops",
"position": 2,
"url": "/collections/womens-tops",
"submenus": []
},
{
"name": "Sweaters & Sweatshirts",
"position": 3,
"url": "/collections/womens-sweaters",
"submenus": []
}
]
I feel like a recursive solution would be best, but I can't get it right. What would be the best approach for this problem?
This should do, assuming you forgot "Womens" in the resulting array in your example:
def flatten(dct, res=None):
if res is None:
res = []
subdct = {}
for k in dct:
v = dct[k]
if k == "submenus":
for e in v:
flatten(e, res)
v = []
subdct[k] = v
res.append(subdct)
return res
to be called like
result = flatten(json_object)

Resources