python: 将字节数组转换为 json 格式时出错

python: Error while converting bytes array to json format

我正在尝试按照 link

将字节数组转换为 json

代码如下:

command = "my command"
    my_bytes_value = self.subprocess_cmd(command)
    print("OUTPUT----")
    print(my_bytes_value)
    my_json = my_bytes_value.decode('utf8').replace("'", '"')
    print('- ' * 20)

    # Load the JSON to a Python list & dump it back out as formatted JSON
    #milestone 1
    data = json.loads(my_json)
    s = json.dumps(data, indent=4, sort_keys=True)
    print(s)

我遇到以下错误,并且正在执行一些打印语句

    File "1.py", line 50, in getlogs
    data = json.loads(my_json)
  File "/usr/local/Cellar/python@3.9/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/python@3.9/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/python@3.9/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
OUTPUT----
b'signing string: (request-target): post /v2/search\ndate: Sat, 04 Jun 2022 05:41:48 GMT\nhost: example.com\nx-content-sha256: askhKJADKJ/Bdm+sdlfkhlkh1376/m6ti5fo=\ncontent-type: application/json\ncontent-length: 342\n[ {\n  "timestamp" : "2022-06-04T05:16:50.522Z",\n  "segmentPartitionId" : null,\n  "component" : null,\n  "region" : null,\n  "availabilityDomain" : null,\n  "filename" : null,\n  "casperUrl" : null,\n  "host" : null,\n  "source" : "EventStats: \n Some message String\nSome message String\n",\n  "fullSource" : "{\"cursor\":-2002349868963,\"bucket\":\"xyz\",\"kafkaAd\":\"gr-10\",\"kafkaOffset\":239846986,\"kafkaPartition\":15,\"kafkaRecord\":273,\"kafkaTopic\":\"logs\",\"lineOffset\":2343,\"logGroup\":\"dev_log\",\"logStreamId\":\"xdfklhklsdhfkl\",\"logType\":\"STANDARD\",\"namespace\":\"dev-api\",\"rawSource\":\"{\\\"logpersisting\\\":\\\"some_logs\\\",\\\"level\\\":\\\"INFO\\\",\\\"logger\\\":\\\"com.example.CollectorTaskHelper\\\",\\\"msg\\\":\\\"LogStats: \\\\n Some message String\\\\nSome message String\\\\n\\\",\\\"ts\\\":1243454354,\\\"workflowid\\\":\\\"12345\\\",\\\"workflowstepname\\\":\\\"LOG_ALERTS\\\"}\",\"resource\":\"askjgjf\",\"tenant\":\"xyz\",\"ts\":123214234,\"tsIndexed\":239486986,\"tsIngested\":324868623,\"#eventpersisting\":\"exadata_events\",\"#level\":\"INFO\",\"#logger\":\"com.example\",\"#thread\":\"flow-Processor-Thread-137\",\"#workflowid\":\"8kjhsdf76784378\",\"#flowname\":\"CollectorWorkflowV2\",\"#flownodename\":\"example.com\",\"#workflowstepname\":\"LOG_ALERTS\"}"\n} ]'


- - - - - - - - - - - - - - - - - - - - 

Process finished with exit code 1

所以,直到里程碑它工作正常但之后就不能工作了。看起来有些角色在这里被宠坏了。但无法识别。

所以,我发现从字节转换为字符串后,第一行不是 json 字符串的一部分,所以我删除了它。之后我可以将它转换成 json

command = "my command"
my_bytes_value = self.subprocess_cmd(command)
print("OUTPUT----")
print(my_bytes_value)
list1 = my_bytes_value.decode('utf8').replace("'", '"').split('\n')[1:]
my_json = ''.join(list1)
print('- ' * 20)

# Load the JSON to a Python list & dump it back out as formatted JSON
#milestone 1
data = json.loads(my_json)
s = json.dumps(data, indent=4, sort_keys=True)
print(s)

它在您的情况下不起作用的原因是因为您的字符串格式不正确 json 即使在进行了您在其他 post.[=11= 中找到的调整之后]

尽管字符串中有一些 json,所以取决于您是否真的需要字符串的完整部分,您可以这样做。

command = "my command"
    my_bytes_value = self.subprocess_cmd(command)
    print("OUTPUT----")
    print(my_bytes_value)
    print('- ' * 20)

    # Load the JSON to a Python list & dump it back out as formatted JSON
    #milestone 1
    text = my_bytes_value.decode('utf-8')
    my_json = json.loads(text[text.index("[") :])
    my_json[0]["fullSource"] = json.loads(my_json[0]["fullSource"])
    my_json[0]["fullSource"]["rawSource"] = json.loads(my_json[0]["fullSource"]["rawSource"])
    print(my_json)
    json.dumps(my_json, indent=4, sort_keys=True)