【问题标题】:Azure Cosmos DB SQL - how to unescape inner json propertyAzure Cosmos DB SQL - 如何取消转义内部 json 属性
【发布时间】:2018-05-10 12:01:52
【问题描述】:

好的,我已经花了几个小时试图让它工作。

我的 json 中有一个内部属性,它是一个 json 对象。但是,当设备向我发送数据时,Gateway_Info 中的 json 是分隔的。这使得查询该内部对象变得不可能(使用点表示法)

有没有办法从该 json 字符串中删除 \ 字符以使其成为有效的 json?

SELECT * FROM c

 {
        "Asset_Key": "1",
        "Defrost_Cycles": 0,
        "Freeze_Cycles": 0,
        "Float_Switch_Raw_ADC": 0,
        "Bin_status": 0,
        "Line_Voltage": 0,
        "ADC_Evaporator_Temperature": 0,
        "Mem_Sw": 0,
        "Freeze_Timer": 0,
        "Defrost_Timer": 0,
        "Water_Flow_Switch": 0,
        "ADC_Mid_Temperature": 0,
        "ADC_Water_Temperature": 0,
        "Ambient_Temperature": 1,
        "Mid_Temperature": 1,
        "Water_Temperature": 1,
        "Evaporator_Temperature": 1,
        "Gateway_Info": "{\"temp_sensor\":0.00,\"temp_pcb\":82.00,\"gw_uptime\":123912.00,\"gw_fw_version\":\"0.0.0\",\"gw_fw_version_git\":\"1-dirty\",\"gw_sn\":\"30\",\"heap_free\":10648.00,\"gw_sig_csq\":19.00,\"gw_sig_quality\":1,\"wifi_sig_strength\":0.00,\"wifi_resets\":0.00,\"modem_sim_iccid\":\"1\",\"modem_meid\":\"1\",\"modem_model\":\"1\"}",
        "ADC_Ambient_Temperature": 0
}

【问题讨论】:

  • 哦,我打算使用插入触发器来更新插入后的值。 (此表中只有插入。
  • 为什么不过滤掉你的payload到达时的``字符?您按原样存储原始内容是否有原因?
  • 我该怎么做?我尝试插入一个 Azure 函数(这是适用的部分): metarequest.on('row', function(columns) { columns.forEach(function(column) { if(column.metadata.colName == "Gateway_Info") { streamInput.Gateway_Info_Formatted = JSON.Parse(column.value); } });asset = mfg+mdl; conditionVariables(asset); }); connection.execSql(metarequest);
  • 当我将它记录到 context.log 时似乎可以工作,但在查询 Cosmos 时它仍然出现转义

标签: json azure-cosmosdb


【解决方案1】:

cosmos db中的PreTrigger需要在代码中定义,因为你的数据是设备发送的,所以不能通过PreTrigger工作。

因此,正如您在评论中提到的,我建议您在将每个文档插入 cosmos db 之前使用 Azure Function CosmosTrigger 对其进行处理。

我的示例文档:

我的 Azure Function CosmosTrigger 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ProcessJson
{
    public class Class1
    {
        [FunctionName("DocumentUpdates")]
        public static void Run(
        [CosmosDBTrigger("db", "item", ConnectionStringSetting = "myCosmosDB")]
        IReadOnlyList<Document> documents,
        TraceWriter log)
        {
            String endpointUrl = "***";
            String authorizationKey = "***";
            String databaseId = "db";
            String collectionId = "item";

            DocumentClient client = new DocumentClient(new Uri(endpointUrl), authorizationKey); ;

            Document doc = documents[0];

            string gateway = doc.GetPropertyValue<string>("gateway");
            JObject o = JObject.Parse(gateway);

            doc.SetPropertyValue("gateway",o);

            client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, doc.Id), doc);

            log.Verbose("document Id " + doc.Id);
        }
    }
}

插入结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2020-10-12
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多