4

I have a big JSON received from another server that I want to do some query operations on it. for example :

{
  "name":"John",
  "age":30,
  "cars": {
    "car1":"Ford",
    "car2":"BMW",
    "car3":"Fiat"
  },
  "friends": ["abby","benji","ciri"]
 }

I can do the queries manually and maybe use lodash, but is there a library to perform queries like: FIND.. WHERE.. , SELECT, TOP 5...

Should I consider using no sql in memory DB for this?

Kamalesh M. Talaviya
  • 1,422
  • 1
  • 12
  • 26
Tomer
  • 119
  • 1
  • 8
  • NoSQL would be the best use for that case. – Marcin Dec 23 '19 at 10:12
  • even if i'm working on just single json document at a time? i don't really need to save that json , just query it and return filtered result – Tomer Dec 23 '19 at 10:14
  • 1
    If you are asking for a library, then this is off-topic. If you have a problem with implementing a solution, please concentrate on one aspect, and describe what you have done so far and where you have a problem. Otherwise this question is too broad. Note that there are several Q&A on this site, on specific aspects, like find, filter, intersect, union, ....etc – trincot Dec 23 '19 at 10:14
  • You can see this : https://stackoverflow.com/questions/8913011/query-javascript-object – Nazmul81 Dec 23 '19 at 10:20

2 Answers2

1

You can try alasql.js where you can do queries over JSON objects.

var data = [{
  "name":"John",
  "age":30,
  "cars": {
    "car1":"Ford",
    "car2":"BMW",
    "car3":"Fiat"
  },
  "friends": ["abby","benji","ciri"]
 },
 {
  "name":"Smith",
  "age":17,
  "cars": {
    "car1":"Ford",
  },
  "friends": ["a","b","c"]
 }]

// Do the query
console.log(alasql("SELECT * FROM ? WHERE age >= 18",[data]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/alasql/0.5.1/alasql.min.js"></script>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

For "querying" collections and array lodash and ramda are appropriated.

For making "real queries", although, you need a db (documental (Eg Mongo), relational (Eg PostGre) or graph (Eg Neo4J), in base of your needs).

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
  • Asking for off-site resources (like libraries) is off-topic for SO. Why do you add an answer anyway? – Andreas Dec 23 '19 at 10:23
  • 1
    This is about problem approach, not particular libraries. Doing SQL-like queries is not common, is still a better option to rely on array/collection libraries or just vanilla javascript. – Mosè Raguzzini Dec 23 '19 at 10:25