Ravi M
Ravi M

Reputation: 81

JS: JSON Data query like SQL

I have the following JSON data

{
  "pebble" {
     "status" : "active"
   },
  "stone" {
     "status" : "active"
   },
  "stone_ny" {
     "status" : "active"
   },
  "stone_london" {
     "status" : "active"
   },
  "stone_tokyo" {
     "status" : "active"
   }
}

In JS, is there a way to get all rows that match stone_.* that returns the last 3 rows?

Upvotes: 2

Views: 1080

Answers (5)

Shahnawaz Hossan
Shahnawaz Hossan

Reputation: 2720

Just use a simple regex to match stone keys by using /stone_/gi in this way:

var names = {
    "pebble": {
        "status": "active"
    },
    "stone": {
        "status": "active"
    },
    "stone_ny": {
        "status": "active"
    },
    "stone_london": {
        "status": "active"
    },
    "stone_tokyo": {
        "status": "active"
    }
}

var matchedNames = {};

for (name in names) {
    if (/stone_/gi.test(name)) {
        matchedNames[name] = names[name];
    }
}

console.log(matchedNames);

Explanation of regex:

  • g = global, match all instances of the pattern in a string, not just one
  • i = case-insensitive (so, for example, /a/i will match the string "a" or "A".

Upvotes: 1

3limin4t0r
3limin4t0r

Reputation: 21130

You can loop through all enumerable string properties (including inherited once) with for...in. You can combine this with a regex match to check if the key meets your requirements.

const data = {
  "pebble"      : { "status": "active" },
  "stone"       : { "status": "active" },
  "stone_ny"    : { "status": "active" },
  "stone_london": { "status": "active" },
  "stone_tokyo" : { "status": "active" },
};

const result = {};
for (const key in data) {
  if (key.match(/^stone_/)) result[key] = data[key];
}

console.log(result);

However if you are currently already using a library you could check if there is a helper present. A common name would be pickBy() which accepts the object and predicate (test function).

In Lodash:

const result = _.pickBy(data, (_, key) => key.match(/^stone_/));

In Ramda:

const result = R.pickBy((_, key) => key.match(/^stone_/), data);
// or: const result = R.pickBy(R.flip(R.test(/^stone_/)), data);

Some libraries might also use filter() and return an object if the input was an object.

Upvotes: 0

tim dowd
tim dowd

Reputation: 1

var data = {
    "pebble": {
        "status": "active"
    },
    "stone": {
        "status": "active"
    },
    "stone_ny": {
        "status": "active"
    },
    "stone_london": {
        "status": "active"
    },
    "stone_tokyo": {
        "status": "active"
    }
}

const query = Object.entries(data).reduce((acc, val)=> {
  return val[0].slice(0, 6) === 'stone_' ? {...acc,  [val[0]]: val[1]} : acc
}, {})

Upvotes: 0

pullidea-dev
pullidea-dev

Reputation: 1803

source={
  "pebble": {
     "status" : "active"
   },
  "stone": {
     "status" : "active"
   },
  "stone_ny" {
     "status" : "active"
   },
  "stone_london": {
     "status" : "active"
   },
  "stone_tokyo": {
     "status" : "active"
   }
};

rows = [];

for(var k in source)
 if(k.substr(0,6)=="stone_")
  rows.push({[k]:source[k]});

Upvotes: 0

Mikkel
Mikkel

Reputation: 7777

It's not as scary as @suchislife makes out. JSON data is inherently flexible in ways that SQL can't even begin to do. That's the reason why it has become so ubiquitous.

Let's say your data is in a variable called data

var data = {
    "pebble": {
        "status": "active"
    },
    "stone": {
        "status": "active"
    },
    "stone_ny": {
        "status": "active"
    },
    "stone_london": {
        "status": "active"
    },
    "stone_tokyo": {
        "status": "active"
    }
}
const result = Object.keys(data)
  .filter(key => key.match(/^stone_/) // This does the filtering (the WHERE clause)
  .map(key => { return {[key]: data[key]}}) // This returns your selected rows

Upvotes: 1

Related Questions