首页 > 解决方案 > 用于输出 JSON 的 Python 脚本的属性值对输出错误

问题描述

我正在编写一个 Python 脚本来将一些 JSON 文件输出到 Azure 上的 Cosmos DB。我的脚本如下所示:

import logging
import uuid
import json

import azure.functions as func


def main(event: func.EventHubEvent, message: func.Out[func.Document]) -> None:
    event_body = event.get_body().decode('utf-8')

    logging.info('Python event trigger function processed an event item: %s',event_body)

    data = {
        "value": event_body,
        "insertion_time": event_body
        }

    message.set(func.Document.from_json(json.dumps(data)))

输出写成:

{
    "value": "{\n  \"value\": \"66\",\n  \"insertion_time\": \"2020-06-02T05:50:00+00:00\"\n}",
    "insertion_time": "{\n  \"value\": \"66\",\n  \"insertion_time\": \"2020-06-02T05:50:00+00:00\"\n}"  
}

但是,我希望它是这样的:

{
    "value": "66",
    "insertion_time": "2020-06-02T05:50:00+00:00"  
}

我该如何纠正?

标签: pythonjsonazure-functions

解决方案


event_body似乎是一个 JSON 字符串,其中已经包含您想要的内容。看来您不需要做任何事情,只需直接使用它:

message.set(func.Document.from_json(event_body))

推荐阅读