当前位置: 首页 > 知识库问答 >
问题:

有效JSON上的JSON解析失败

笪俊迈
2023-03-14

下面是我的logstash.conf文件:

input {
    http {
        host => "127.0.0.1"
        port => 31311 
        ssl => false
    }
}

filter {
    mutate {
        split => { "[headers][request_path]" => "/"}
        add_field => { "index_id" => "%{[headers][request_path][1]}" }
    }

    ruby { 
        code => "event.set('request_path_length', event.get('[headers][request_path]').length)" 
    }

    if [request_path_length] == 3 {
        mutate {
            add_field => { "document_id" => "%{[headers][request_path][2]}" }
        }
    }
}

output {
    stdout {
        codec => "json"
    }

    if [request_path_length] == 3 {
        elasticsearch {
            hosts => "http://localhost:9200"
            index => "%{index_id}"
            document_id => "%{document_id}"
        }
    }
    else {
        elasticsearch {
            hosts => "http://localhost:9200"
            index => "%{index_id}"
        }
    }
}
PS C:\Users\Me\Downloads\curl-7.64.1-win64-mingw\bin> .\curl.exe -H "content-type:application/json" -XPUT 'http://127.0.0.1:31311/bo
ok/1' -d @"
>> {"IndexId":"Book","DocumentId":"0","Title":"EKQXAQQSZAAUKVLKVHXAB","Edition":1,"Author":{"Name":"Mark Twain","Nationality":"France"},"Pu
blisher":{"Name":"Harper Collins","HeadquartersAddress":{"Country":"Denmark","StreetName":"KRQAQIKTUTMTZ","PostalCode":"4539","State":"WOOS
ESVGWIJDZSZ"}}}
>> "@
[2019-04-10T12:36:00,232][ERROR][logstash.codecs.json     ] JSON parse error, original data now in message field {:error=>#<LogStash::Json::ParserError: Unexpected character ('I' (code 73)): was expecting double-quote to start field name
 at [Source: (String)"{IndexId:Book,DocumentId:0,Title:EKQXAQQSZAAUKVLKVHXAB,Edition:1,Author:{Name:Mark Twain,Nationality:France},Publisher:{Name:Harper Collins,HeadquartersAddress:{Country:Denmark,StreetName:KRQAQIKTUTMTZ,PostalCode:4539,State:WOOSESVGWIJDZSZ}}}"; line: 1, column: 3]>, :data=>"{IndexId:Book,DocumentId:0,Title:EKQXAQQSZAAUKVLKVHXAB,Edition:1,Author:{Name:Mark Twain,Nationality:France},Publisher:{Name:Harper Collins,HeadquartersAddress:{Country:Denmark,StreetName:KRQAQIKTUTMTZ,PostalCode:4539,State:WOOSESVGWIJDZSZ}}}"}
{"message":"{IndexId:Book,DocumentId:0,Title:EKQXAQQSZAAUKVLKVHXAB,Edition:1,Author:{Name:Mark Twain,Nationality:France},Publisher:{Name:Harper Collins,HeadquartersAddress:{Country:Denmark,StreetName:KRQAQIKTUTMTZ,PostalCode:4539,State:WOOSESVGWIJDZSZ}}}","document_id":"1","@version":"1","@timestamp":"2019-04-10T12:36:00.233Z","index_id":"book","request_path_length":3,"tags":["_jsonparsefailure"],"headers":{"content_type":"application/json","request_path":["","book","1"],"http_accept":"*/*","http_version":"HTTP/1.1","content_length":"242","request_method":"PUT","http_user_agent":"curl/7.64.1","http_host":"127.0.0.1:31311"},"host":"127.0.0.1"}

更新:

我运行了一个稍微不同的命令:

PS c:\users\me\downloads\curl-7.64.1-win64-mingw\bin>.\curl.exe-h“content-type:application/json”-xput'http://127.0.0.1:31311/bo ok/1'-d'{\“indexid\”:\“book\”,\“documentid\”:\“0\”,\“title\”:\“ekqxaqqszaaukvhxab\”,\“edition\”:\“1\”,\“作者\”:{\“name\”:\“mark Twai n\”,\“国籍\”:“:\”哈珀·柯林斯\“,\”总部地址\“:{\”国家\“:\”丹麦\“,\”街名\“:\”krqaqiktutmtz\“,\”邮政编码\“:\”4539\“,\”州\“:\”woosesvgwijdzsz\“}}}‘

这一次,我在LogStash终端中看到了这个错误消息:

[2019-04-10T12:53:03,799][ERROR][logstash.codecs.json     ] JSON parse error, original data now in message field {:error=>#<LogStash::Json::ParserError: Unexpected end-of-input: was expecting closing quote for a string value
 at [Source: (String)"{"IndexId":"Book","DocumentId":"0","Title":"EKQXAQQSZAAUKVLKVHXAB","Edition":"1","Author":{"Name":"Mark"; line: 1, column: 207]>, :data=>"{\"IndexId\":\"Book\",\"DocumentId\":\"0\",\"Title\":\"EKQXAQQSZAAUKVLKVHXAB\",\"Edition\":\"1\",\"Author\":{\"Name\":\"Mark"}
{"message":"{\"IndexId\":\"Book\",\"DocumentId\":\"0\",\"Title\":\"EKQXAQQSZAAUKVLKVHXAB\",\"Edition\":\"1\",\"Author\":{\"Name\":\"Mark","tags":["_jsonparsefailure"],"@version":"1","@timestamp":"2019-04-10T12:53:03.813Z","headers":{"http_version":"HTTP/1.1","http_user_agent":"curl/7.64.1","content_length":"103","request_method":"PUT","http_accept":"*/*","request_path":["","book","1"],"content_type":"application/json","http_host":"127.0.0.1:31311"},"host":"127.0.0.1","index_id":"book","document_id":"1","request_path_length":3}

共有1个答案

胡玉书
2023-03-14

尝试使用:

{\"IndexId\":\"Book\",\"DocumentId\":\"0\",\"Title\":\"EKQXAQQSZAAUKVLKVHXAB\",\"Edition\":\"1\",\"Author\":{\"Name\":\"Mark Twai n\",\"Nationality\":\"France\"},\"Publisher\":{\"Name\":\"Harper Collins\",\"HeadquartersAddress\":{\"Country\":\"Denmark\",\"StreetName\":\"KRQAQIKTUTMTZ\",\"PostalCode\":\"4539\",\"State\":\"WOOSESVGWIJDZSZ\"}}}

注意France之前的双引号没有转义,并且在第一个双引号之前的StreetName值中有一个错误的空格。

 类似资料:
  • 我在从客户端代码调用RESTful服务时遇到了一个问题。我已经使用CXF/Jackson编写了RESTful服务,部署到localhost中,并成功地使用RESTClient进行了测试。下面是服务代码的一个片段: 模型类和dao可以成功地工作,使用RESTClient服务本身也可以很好地工作。但是,当我试图从Java脚本调用这个服务时,我在服务器端得到以下错误: 公共类情绪计{ }

  • 我尝试在https://flutter.io/cookbook/networking/background-parsing/的后台实现解析JSON,我得到了下面的错误。当我使用Android模拟器时,错误也相同。 我用的是macOs HighSierra。我安装IntelliJ和Fluter和黑暗插件。我在Flutter上测试了大多数例子,结果都很好。我还创建了一个演示和它的工作。我不明白为什么这

  • 问题内容: 我正在提取无效的JSON JSON提要。它完全缺少引号。我尝试了一些操作,例如和,以使该字符串看起来更像有效的JSON,但是在其中包含关联JSON字符串的情况下,通常会弄糟。 这是一个例子: 是否有任何php的JSON解析器可以处理这样的无效JSON? 编辑:我试图在此字符串上使用。它什么也不返回。 问题答案: 所有引号应为双引号而不是单引号。 所有键都应加引号。 整个元素应该是一个对

  • json 库能够解析字符串或文本中的 JSON 内容。 该库将 JSON 解析为 Python 字典或列表,也能将 Python 字典或列表转换为 JSON 字符串。 解析 JSON 如下的 JSON 格式的字符串: json_string = '{"first_name": "Guido", "last_name":"Rossum"}' 如下代码能够将其解析: import json par

  • 问题内容: 我试图从JSON数组中获取每个JSON对象。我通过HTTP发布获得此数据。 我知道我的数据是什么样的: 我的示例代码和结构如下所示: 我不确定如何遍历JSON数组并获取JSON对象,然后仅使用JSON对象。 问题答案: 试试这个作为您的结构, 您的名称不正确,顶层名称也不正确。解码为a之后,您可以遍历切片以获取每个切片