Building and using a concise json db in PHP and in javascript
I saw many questions about json in stackoverflow. Most of them are unanswered or the basic idea is relaying on already existing techiniques.
I want to build json db to use it in a easy way as a query usage. Like SELECT a WHERE a = $var;
Please your suggestions.
Thanks in advance. //sample jsondb
{
"name": "test",
"columns": ["a", "b"],
"rows": [[1, 2],
[3, 4]]
}
$var = 3;
//the aim is to use it easy as query usage
SELECT a WHERE a = $var;
//sample json object retrieved by PHP's json_encode()
stdClass Object
(
[name] => test
[columns] => Array
(
[0] => a
[1] => b
)
[rows] => Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
)
)
//have the column a
$cols = array_flip($obj->columns);
$col_a = $cols['a'];
//filter to a=$var
$rows_with_value_3 = array();
foreach($obj->rows as $index => $rowvalues){
foreach($rowvalues as $value){
if($value[$col_a] == $var)
$rows_with_value_3[$index]开发者_如何学Go = $value[$col_a];
}
}
//below the query string build functions
....
JSON Query Links:
- JSONPath
- JPath
- JSONQuery
- SQLike
- JSLinq
- JSinq
Check Databases using JSON as storage/transport format for JSON Databases.
You can add SculeJS to the above list. It does what you're looking for using a MongoDB style query interface, and it's written in JavaScript.
精彩评论