0

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

asked Jul 31, 2018 at 10:29
2
  • 1
    If the above is stored in obj, you can access that part of it using obj.links or obj['links'] Commented Jul 31, 2018 at 10:31
  • Also: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… and googling "read part of json data" gives loads of relevant results Commented Jul 31, 2018 at 10:42

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the very late reply to this. Thanks this has helped.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.