【问题标题】:Parse value from json从 json 解析值
【发布时间】:2021-12-14 21:00:15
【问题描述】:

对于特定服务的日志,我的一个属性 httpRequest 返回一个 json 对象:

"httpRequest": {
    "country": "DE",
    "headers": [
        {
            "name": "Host",
            "value": "mydomain.example.com"
        },
        {
            "name": "User-Agent",
            "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:94.0) Gecko/20100101 Firefox/94.0"
        },
        {
            "name": "Accept",
            "value": "image/avif,image/webp,*/*"
        },
        {
            "name": "Accept-Language",
            "value": "en,en-US;q=0.5"
        },
        {
            "name": "Accept-Encoding",
            "value": "gzip, deflate, br"
        },
        {
            "name": "Connection",
            "value": "keep-alive"
        },
        {
            "name": "Referer",
            "value": "https://mydomain.example.com/cat.jpg"
        },
        {
            "name": "Cookie",
            "value": "OptanonConsent=isIABGlobal=false&datestamp=Thu+Oct+21+2021+09%3A55%3A11+GMT%2B0200+(South+Africa+Standard+Time)&version=6.22.0&landingPath=NotLandingPage&AwaitingReconsent=false&groups=C0001%3A1%2CC0003%3A1%2CC0002%3A1%2CC0004%3A1&isGpcEnabled=0&hosts=&geolocation=ZA%3BWC; _ga_QQSC9D30C7=GS1.1.1620799882.6.0.1620799882.60; _ga=GA1.2.2133505955.1614865120; WZRK_G=79ce4c41d4194196af7e2846c4baec9b; ajs_anonymous_id=%227acfac50-a8df-4199-921c-eb149f78cd8d%22; OptanonAlertBoxClosed=2021-10-21T07:55:10.937Z"
        },
        {
            "name": "Sec-Fetch-Dest",
            "value": "image"
        },
        {
            "name": "Sec-Fetch-Mode",
            "value": "no-cors"
        },
        {
            "name": "Sec-Fetch-Site",
            "value": "same-origin"
        },
        {
            "name": "Pragma",
            "value": "no-cache"
        },
        {
            "name": "Cache-Control",
            "value": "no-cache"
        }
    ],
    "args": "",
    "httpVersion": "HTTP/1.1"
}

我要做的是解析headers 部分,以便我可以为headers.Host 值创建一个新方面。还有一点需要注意的是,这个headers数组中的对象数量是可以变化的,即有时候会有一个X-Forwarder-for的头部作为例子。

为了创建一个新属性,我尝试使用 String builder 处理器 - 但它看起来不适用于数组。我尝试使用 %{httpRequest.headers.Host} 的目标值,但它返回一个空值。

【问题讨论】:

    标签: datadog


    【解决方案1】:

    可以使用过滤方法:

    const req={"httpRequest": { "country": "DE", "headers": [ { "name": "Host", "value": "mydomain.example.com" }, { "name": "User-Agent", "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:94.0) Gecko/20100101 Firefox/94.0" }, { "name": "Accept", "value": "image/avif,image/webp,*/*" }, { "name": "Accept-Language", "value": "en,en-US;q=0.5" }, { "name": "Accept-Encoding", "value": "gzip, deflate, br" }, { "name": "Connection", "value": "keep-alive" }, { "name": "Referer", "value": "https://waftestcdn.getsmarter.com/cat.jpg" }, { "name": "Cookie", "value": "OptanonConsent=isIABGlobal=false&datestamp=Thu+Oct+21+2021+09%3A55%3A11+GMT%2B0200+(South+Africa+Standard+Time)&version=6.22.0&landingPath=NotLandingPage&AwaitingReconsent=false&groups=C0001%3A1%2CC0003%3A1%2CC0002%3A1%2CC0004%3A1&isGpcEnabled=0&hosts=&geolocation=ZA%3BWC; _ga_QQSC9D30C7=GS1.1.1620799882.6.0.1620799882.60; _ga=GA1.2.2133505955.1614865120; WZRK_G=79ce4c41d4194196af7e2846c4baec9b; ajs_anonymous_id=%227acfac50-a8df-4199-921c-eb149f78cd8d%22; OptanonAlertBoxClosed=2021-10-21T07:55:10.937Z" }, { "name": "Sec-Fetch-Dest", "value": "image" }, { "name": "Sec-Fetch-Mode", "value": "no-cors" }, { "name": "Sec-Fetch-Site", "value": "same-origin" }, { "name": "Pragma", "value": "no-cache" }, { "name": "Cache-Control", "value": "no-cache" } ], "args": "", "httpVersion": "HTTP/1.1" }}
    
    console.log(req.httpRequest.headers.filter((el)=>el.name==='Host')[0].value)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-30
      • 2018-04-13
      • 2022-11-26
      • 2019-11-27
      • 1970-01-01
      • 2016-08-15
      • 2012-04-12
      • 2019-10-25
      相关资源
      最近更新 更多