【问题标题】:Problem with repeated objects while importing JSON into Google BigQuery将 JSON 导入 Google BigQuery 时出现重复对象的问题
【发布时间】:2021-03-24 15:22:28
【问题描述】:

我一直在尝试将 JSON 手动上传到 BigQuery,但收到以下错误消息。

Error while reading data, error message: JSON parsing error in row starting at position 0: Repeated field must be imported as a JSON array. Field: custom_fields.value.

我已经将文件转换为换行符分隔的 JSON,所以这不是问题。 从错误中查看 custom_field.value 时,我得到以下信息:

$ cat convert2working.json | jq .custom_fields[].value
0
"Basics of information security\n"
"2021"

问题似乎是custom_fields.value的数据类型不同。

如何“同质化”这些数据类型?还是您有其他解决方案。我宁愿留在 javascript 中

这是我的 JSON 代码的缩短版本:

{
    "id": "example",
    "custom_fields": [
        {
            "id": "example",
            "name": "Interval",
            "type": "drop_down",
            "type_config": {
                "default": 0,
                "placeholder": null,
                "options": [
                    {
                        "id": "example",
                        "name": "yearly",
                        "color": null,
                        "orderindex": 0
                    }
                ]
            },
            "date_created": "1611228211303",
            "hide_from_guests": false,
            "value": 0,
            "required": false
        },
        {
            "id": "example",
            "name": "Description",
            "type": "text",
            "type_config": {},
            "date_created": "1611228263444",
            "hide_from_guests": false,
            "value": "Basics of information security\n",
            "required": false
        },
        {
            "id": "example",
            "name": "Year",
            "type": "number",
            "type_config": {},
            "date_created": "1611228275285",
            "hide_from_guests": false,
            "value": "2021",
            "required": false
        }
    ]
}

【问题讨论】:

  • 您应该与他人分享您当前的 javascript 代码,以便他们更好地了解您的问题并帮助您解决问题。
  • 我还没有 javascript 代码,因为我想看看是否可以先手动将其上传到 BigQuery。
  • 我只是您帖子的审阅者,从未听说过 BigQuery。但我对返回消息的理解是,您将 Bi​​gQuery 中的字段值定义为重复字段,因此,google 期望您在 value 中的数据是一个数组而不是字符串。所以我想说:它看起来像是大查询中的数据结构定义中的一个问题。我认为您可以通过更改其中的字段配置或将value 包装在[] 之间来解决您的问题。 "value": ["2021"],

标签: javascript json google-bigquery


【解决方案1】:

您需要规范化数据结构,以便 BigQuery 能够auto-detect 一致的架构。因为 value 属性曾经是 numberstring,所以此自动检测失败。

有多种方法可以标准化您的数据。我不能 100% 确定哪种方式最适合 BigQuery,它声称最多可以分析前 100 行以进行架构自动检测。

第一次尝试是将不同类型的值放到不同的字段中

const fields = data.custom_fields.map(x => {

    const f = {
        id: x.id,
    name: x.name
  };
  
  f[x.type] = x.value;
  
  return f;
});

这将产生:

[{
  id: "example",
  name: "Interval",
  value_drop_down: 0
}, {
  id: "example",
  name: "Description",
  value_text: "Basics of information security↵"
}, {
  id: "example",
  name: "Year",
  value_number: "2021"
}]

我不确定这是否是 BigQuery 可以可靠地合并字段的推断类型架构的结构,因为它可能例如仅在前 100 行中遇到 value_number,因此不会处理 value_dropdown

一种更可靠的方法(假设您知道type 的所有不同值)是将记录显式转换为相同的结构。这还具有能够对字段值运行任何特殊转换(例如转换、查找等)的优势

const fields2 = data.custom_fields.map(x => ({
    id: x.id,
    name: x.name,
    value_text: x.type === 'text' ? x.value : null,
    value_number: x.type === 'number' ? parseInt(x.value, 10) : null,
    value_dropdown: x.type === 'drop_down' ? x.type_config.options.find(o => o.orderindex === x.value).name : null
  })
);

根据您的数据(例如,如果值是可选的或可以为空),您可能必须使一些转换逻辑更加健壮。使用您的示例数据,此转换产生:

[{
  "id": "example",
  "name": "Interval",
  "value_text": null,
  "value_number": null,
  "value_dropdown": "yearly",
  "value_drop_down": 0
}, {
  "id": "example",
  "name": "Description",
  "value_text": "Basics of information security\n",
  "value_number": null,
  "value_dropdown": null
}, {
  "id": "example",
  "name": "Year",
  "value_text": null,
  "value_number": "2021",
  "value_dropdown": null
}]

我创建了一个JSFiddle,您可以在其中玩弄这段代码。

【讨论】:

    猜你喜欢
    • 2017-09-05
    • 2023-02-09
    • 1970-01-01
    • 2019-01-27
    • 2021-12-05
    • 1970-01-01
    • 2021-06-27
    • 2012-10-27
    • 1970-01-01
    相关资源
    最近更新 更多