Tom
Tom

Reputation: 1628

Reading specific JSON data

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

Upvotes: 0

Views: 39

Answers (1)

Ankit Agarwal
Ankit Agarwal

Reputation: 30729

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]);
});

Upvotes: 2

Related Questions