0

All I am trying to do is reading an JSON file with data format as shown below and extract only id from nodes and store it in an array.

{"nodes":[
{"id":"1057457211927117824", "age":"20", "name":"a", "loaded":true},
{"id":"1057459284189970433", "age":"20", "name":"b", "loaded":true}
]
"links":[
{"id":"l01", "from":"1057457210467540992", "to":"1057455883972722689", "type":"friend"},
{"id":"l02", "from":"1057457271331057664", "to":"1057451606344646656", "type":"friend"}
]}

Below is the code I tried but its not working.

var node_id = []; 
$.getJSON("data/newData.json", function (data) {
    $.each(data, function (index, value) {
        node_id.push(value[0]['id']); 
    });
});

1 Answer 1

1

If I understand you, you can look at the code below:

var jsonData = {
"nodes":[
  {"id":"1057457211927117824", "age":"20", "name":"a", "loaded":true},
  {"id":"1057459284189970433", "age":"20", "name":"b", "loaded":true}],
"links":[
  {"id":"l01", "from":"1057457210467540992", "to":"1057455883972722689", "type":"friend"},
  {"id":"l02", "from":"1057457271331057664", "to":"1057451606344646656", "type":"friend"}
]};

var node_id = []; 
$.each(jsonData.nodes, function (index, value) {
    node_id.push(value['id']); 
});

console.log(node_id);
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what i was trying to do.Thanks a lot.

Your Answer

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.