Mason
Mason

Reputation: 1

Count how many times a specific json/word appears

If I have a website that contains JSON with multiple "usrId":, how can I count how many times this exists? I do not know if it's better to count JSON objects or just count how many times that word exists on the webpage, also I wouldn't know how to get started, how can I do this?

request(getusr, function(err, res, body) {
  let json = JSON.parse(body);
  let usramount = // count how many times usrId appears
})

Example json:

  {  
     "id":"example",
     "usrId":"example"
  },
  {  
     "id":"example"
     "usrId":"example",
  },

Upvotes: 0

Views: 209

Answers (1)

blue112
blue112

Reputation: 56462

(body.match(/"usrId":/g) || []).length;

should do the trick.

Upvotes: 2

Related Questions