首页 > 解决方案 > Microsoft Excel - 从 JSON 列中获取值

问题描述

我们正在使用 Azure APIM 服务和链接的 App Insights 来获取请求日志以进行分析。

当我们使用 Imperva WAF 时,我们正在通过 App Insights Logs(Analytics)获取 Imperva IP 并在请求标头中获取客户端的 IP。

所以以 csv 格式获取数据,就像这样

"2019-10-06T17:21:20.2264252Z","|key",,"OPTIONS /Endpoint/","https://APIM/query",True,200,"0.3565","<250ms",request,"{""ApimanagementServiceName"":""APIM_name"",""ApimanagementRegion"":""Country"",""HTTP Method"":""OPTIONS"",""API Name"":""API"",""Cache"":""None"",""Request-Incap-Client-IP"":""2a:23c4:1c4c:6c00:a458_IP""}","{""Client Time (in ms)"":0,""Response Size"":334,""Request Size"":0}","OPTIONS /EndPoint/",Key,,,,,,,PC,,,"0.0.0.0",,,"Country",,"APIM Country","APIM Country","key","App Insights","key","apim:0.81.21.0","key",1

现在我想从“Request-Incap-Client-IP”元素中提取 IP,该元素以 JSON 格式存储,从“ApimanagementServiceName”开始。

我在网上查看了帮助,都在谈论宏,自定义代码。

在我看来,excel 应该具有解析 json 并从特定列中获取值的功能,我的意思是解决方案应该简单明了。

标签: jsonexcelparsing

解决方案


使用该自定义库来解析/读取/写入 JSON 文件和流

https://github.com/VBA-tools/VBA-JSON

Dim fso As New FileSystemObject
Dim JsonTS As TextStream
Dim Json As Dictionary
Dim JsonText As String

Set JsonTS = fso.OpenTextFile("yourfile.csv", ForReading)
JsonText = JsonTS.ReadAll
JsonTS.Close
'process the string to isolate JSON part of your CSV using Split/regexp
Set Json = JsonConverter.ParseJson(JsonText)

然后你可以像这样检索值:

Dim ipValue as String

ipValue = Json("Request-Incap-Client-IP")

推荐阅读