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?

4
  • NoSQL would be the best use for that case. Commented Dec 23, 2019 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 Commented Dec 23, 2019 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 Commented Dec 23, 2019 at 10:14
  • You can see this : stackoverflow.com/questions/8913011/query-javascript-object Commented Dec 23, 2019 at 10:20

2 Answers 2

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>

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

2 Comments

Asking for off-site resources (like libraries) is off-topic for SO. Why do you add an answer anyway?
600+kb library for this kind of simple task can be an overkill, IMHO. Nice discover for me, anyway :)
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).

2 Comments

Asking for off-site resources (like libraries) is off-topic for SO. Why do you add an answer anyway?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.