如何从 JSON SQL SELECT 并动态分配给变量

How to SQL SELECT from JSON and dynamically assign to variables

TL;DR - 我想 SQL 从 "test.json" 的格式(下面的示例)我想动态分配每个变量,以便根据那里的条目数量它会输出那个数量吗(所以它看起来像下面的输出示例)

我在一个名为 "test.json"

的单独文件中
{
    "12321534232354": {
        "server": "2343262364124",
        "time": "infinite",
        "reason": "not provided"
    },
    "155149108183695360": {
        "server": "2343262364124",
        "time": "infinite",
        "reason": "not provided"
    }
}

我使用 alasq 模块从这个文件 SQL select 像这样:

const alasql = require("alasql");
testfile = require("./test.json");
console.log(alasql("SELECT * FROM ? WHERE server='2343262364124'",[testfile]));

但是,这个 returns [] 而不是我 select 编辑的值。我的最终目标是能够 select 特定服务器并获取每个条目的第一行(在本例中为“12321534232354”、“155149108183695360”)以及 "time" 和 "reason" 并根据数量将它们分配给 variables/an 数组。所以我可以 console.log 所有这些值

期望的结果: 例如:

"test.json" 包含:

{
    "15432213213564": {
        "server": "45645645654645",
        "time": "infinite",
        "reason": "not provided"
    },
    "4567863243123": {
        "server": "45645645654645",
        "time": "infinite",
        "reason": "not provided"
    },
    "5763542345345": {
        "server": "45645645654645",
        "time": "20",
        "reason": "test"
    }
}

如果我查询服务器“45645645654645”,我希望输出为:(例如:根据这个例子)

the user is 12321534232354
the time is infinite
the reason is not provided

the user is 155149108183695360
the time is infinite
the reason is not provided

the user is 5763542345345
the time is 20
the reason is test

这就是为什么我认为我应该将用户、时间、原因动态分配给变量的原因?

请告诉我如何做到这一点,或者更好的代码示例。

谢谢。

我在想这样的事情。因为你有一个 json 文件并且想要 console.log 一个 javascript 对象,所以对我来说只使用 javascript 来获取该对象而不是使用 SQL.

/* Uncomment this so you use the actual json file
const testfile = require("./test.json");
const records = JSON.parse( testfile );
*/
// replace the following records definition by the code above
const records = {
    "15432213213564": {
        "server": "1234756783612",
        "time": "infinite",
        "reason": "not provided"
    },
    "4567863243123": {
        "server": "2343262364124",
        "time": "infinite",
        "reason": "not provided"
    },
    "5763542345345": {
        "server": "2343262364124",
        "time": "20",
        "reason": "test"
    }
};

const findAllServers = ( id_server, records ) => Object.values( records ).filter( record => record.server === id_server );
const all_servers_2343262364124 = findAllServers ( '2343262364124', records );

// will log an array
console.log( all_servers_2343262364124 );

您必须post 处理从json 文件加载的对象。我构建了一个工作样本。我认为 alasql 无法处理该对象,因为它不是数组,实际上它是键值对字典,我的这段代码删除了键,并制作了一个值数组,然后我能够 select:

const alasql = require("alasql");
var testfile = require("./test.json");
//get array of values of the object loaded from file
var values =Object.values(testfile);
//select
console.log(alasql("SELECT * FROM ? WHERE server = \"45645645654645\"", [values]));

更新:用 Object.values()

简化了它

我处理了 test.json 的内容:

{
    "15432213213564": {
        "server": "45645645654645",
        "time": "infinite",
        "reason": "not provided"
    },
    "4567863243123": {
        "server": "45645645654645",
        "time": "infinite",
        "reason": "not provided"
    },
    "5763542345345": {
        "server": "45645645654645",
        "time": "20",
        "reason": "test"
    }
}

你也可以尝试使用AlaSQL的SEARCH运算符:

    const data = require('./test606a.json');
    let res = alasql('SEARCH AS @a \
        KEYS() AS @b \
        EX(@a->(@b)) \
        WHERE(server = ?) \
        RETURN(@b AS user, time, reason)\
        FROM ?',["45645645654645",data]);

SEARCH 运算符旨在遍历复杂的 JSON 结构。

这里:

  • AS @a - 将当前对象保存到变量 a
  • KEYS() - 遍历键
  • AS @b - 将每个密钥保存到变量 b
  • EX(@a->(@b)) - 获取键的值
  • WHERE(server=?) - 仅传递具有所需服务器参数的记录
  • RETURN(...) - 创建输出结构
  • 来自? - 标准来自条款