BigQuery JSON 结果返回 DateTime 值作为具有“value”属性的对象

问题描述 投票:0回答:1

当我使用 @google-cloud/bigquery npm 包对 BigQuery 执行简单查询时,任何 DateTime 类型的列都会作为具有“value”属性的对象返回,如下所示...

{
    "id": "B4BCEEB7-BB95-4163-8B22-C81588682AEC",
    "aString": "37SBAUL4464",
    "effectiveDate": {
        "value": "1970-01-01T00:00:00"
    }
}

这很麻烦。有没有办法禁用此行为或让 DateTime 属性简单地返回其值而不破坏输出格式?

所需的格式如下 - 缺少值的随机对象插入...

{
    "id": "B4BCEEB7-BB95-4163-8B22-C81588682AEC",
    "aString": "37SBAUL4464",
    "effectiveDate": "1970-01-01T00:00:00"
}

这是我的代码的相关部分...

import {BigQuery} from '@google-cloud/bigquery'; // at the top of the file

const bigQueryClient = new BigQuery();
const query = `
    select
        p.PolicyID as id,
        p.SomeString as aString,
        p.EffectiveDate as effectiveDate
    from
        `my-dataset-11111.2222222222__001.Policy` as p
`;

const options = {
    query: query,
    location: config.bigQueryLocation,
};

const [rows] = await bigQueryClient.query(options);
node.js google-bigquery
1个回答
0
投票

这是 BigQuery Node.js 库处理某些数据类型(例如 DateTime)的默认方式,这些数据类型作为特殊包装对象返回。实际上,BigQuery 客户端库中没有直接标志来禁用这种包装。我认为您可以做的最好方法是创建一个函数来递归遍历结果对象并解开值。 import {BigQuery} from '@google-cloud/bigquery'; // ... (your existing code) function unwrapBigQueryResults(rows) { return rows.map(row => { const newRow = {}; for (const key in row) { if (row[key] && typeof row[key] === 'object' && 'value' in row[key]) { newRow[key] = row[key].value; } else if (Array.isArray(row[key])) { newRow[key] = row[key].map(item => unwrapBigQueryResults([item])[0]); } else { newRow[key] = row[key]; } } return newRow; }); } const [rows] = await bigQueryClient.query(options); const unwrappedRows = unwrapBigQueryResults(rows); console.log(unwrappedRows);

	
© www.soinside.com 2019 - 2024. All rights reserved.