Psl
Psl

Reputation: 3920

parse a values from json data

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

Upvotes: 1

Views: 49

Answers (2)

lbrazier
lbrazier

Reputation: 2654

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

Upvotes: 2

adeneo
adeneo

Reputation: 318222

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

Upvotes: 1

Related Questions