1

Am trying to get values from a json data using node.js

Following is the json data(It is dynamic), it may contains names like tcp,ipp,http,udp,https .here i only displaying tcp and ipp only

sample json

[ { '$':
     { name: 'tcp',
       showname: 'tnternet crinting Protocol',
       size: '584',
       pos: '202' },
    field: [ [Object], [Object], [Object], [Object] ] },
  { '$':
     { name: 'ipp',
       showname: 'Internet Printing Protocol',
       size: '584',
       pos: '202' },
    field: [ [Object], [Object], [Object], [Object], [Object], [Object] ] } ]

i need to get the details of ipp only .

(any other methods without using sample[1] ,like name =ipp from the json data)

example

{ '$':
   { name: 'ipp',
     showname: 'Internet Printing Protocol',
     size: '584',
     pos: '202' },
  field:
   [ { '$': [Object] },
     { '$': [Object] },
     { '$': [Object] },
     { '$': [Object], field: [Object] },
     { '$': [Object], field: [Object] },
     { '$': [Object] } ] }

2 Answers 2

2

You could do it using lodash as follows:

  var result = _.result(_.find(list, { '$': {'name':'ipp'} }), '$');

Plunker: http://plnkr.co/edit/unh9wZuDCnIETE7EWHmD?p=preview

Lodash: https://lodash.com/docs#find

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

Comments

1

You'd have to iterate, and look for the name

var ipp = null;

sample.forEach(function(o) {
    if (  o['$'].name === 'ipp' ) {
        ipp = o;
        return false;
    }
});

// ipp === object here

FIDDLE

Comments

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.