I'm trying to process some JSON data and read specific parts of it. This is an example of the output I have :
{
"operators": {
"operator1": {
"top": 20,
"left": 20,
"properties": {
"title": "Input 1",
"inputs": {},
"outputs": {
"output_1": {
"label": "Output 1"
}
}
}
},
"operator2": {
"top": 80,
"left": 300,
"properties": {
"title": "operator2",
"inputs": {
"input_1": {
"label": "Input 1"
}
},
"outputs": {
"output_1": {
"label": "out-1"
},
"output_2": {
"label": "out-2"
}
}
}
},
"created_op_0": {
"top": 60,
"left": 500,
"properties": {
"title": "title",
"inputs": {
"input_1": {
"label": "Input 1"
}
},
"outputs": {}
}
}
},
"links": {
"0": {
"fromOperator": "operator1",
"fromConnector": "output_1",
"fromSubConnector": 0,
"toOperator": "operator2",
"toConnector": "input_1",
"toSubConnector": 0
},
"1": {
"fromOperator": "operator2",
"fromConnector": "output_1",
"fromSubConnector": 0,
"toOperator": "created_op_0",
"toConnector": "input_1",
"toSubConnector": 0
}
},
"operatorTypes": {}
}
I want to read the links only.
I've not been able to find any examples on how to do this. Can anyone point me in the right direction.
How do I read and loop through just that using javascript /jquery ?
Thanks
1 Answer 1
You can loop over to the keys of links property to get all the objects inside links object:
var data = {
"operators": {
"operator1": {
"top": 20,
"left": 20,
"properties": {
"title": "Input 1",
"inputs": {},
"outputs": {
"output_1": {
"label": "Output 1"
}
}
}
},
"operator2": {
"top": 80,
"left": 300,
"properties": {
"title": "operator2",
"inputs": {
"input_1": {
"label": "Input 1"
}
},
"outputs": {
"output_1": {
"label": "out-1"
},
"output_2": {
"label": "out-2"
}
}
}
},
"created_op_0": {
"top": 60,
"left": 500,
"properties": {
"title": "title",
"inputs": {
"input_1": {
"label": "Input 1"
}
},
"outputs": {}
}
}
},
"links": {
"0": {
"fromOperator": "operator1",
"fromConnector": "output_1",
"fromSubConnector": 0,
"toOperator": "operator2",
"toConnector": "input_1",
"toSubConnector": 0
},
"1": {
"fromOperator": "operator2",
"fromConnector": "output_1",
"fromSubConnector": 0,
"toOperator": "created_op_0",
"toConnector": "input_1",
"toSubConnector": 0
}
},
"operatorTypes": {}
};
Object.keys(data.links).forEach(function(key){
console.log(data.links[key]);
});
answered Jul 31, 2018 at 10:32
Ankit Agarwal
30.8k5 gold badges41 silver badges63 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Tom
Sorry for the very late reply to this. Thanks this has helped.
lang-js
obj, you can access that part of it usingobj.linksorobj['links']