如何在python中检查字符串是否是有效的JSON

如何在python中检查字符串是否是有效的JSON,python,json,linux,Python,Json,Linux,我有一个python脚本,在远程linux的控制台中提供命令行/输出。 我有另一个脚本正在本地机器上读取这个输出。 输出格式如下: ABC: NEG BCD: NEG FGH: POS {aa:bb:cc:dd:ee{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity"

我有一个python脚本,在远程linux的控制台中提供命令行/输出。 我有另一个脚本正在本地机器上读取这个输出。 输出格式如下:

ABC: NEG
BCD: NEG
FGH: POS
{aa:bb:cc:dd:ee{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}}
注意最后一行是json格式的,现在我想检查哪一行是json格式的输出。 我试过了

它不是阅读,甚至是阅读

json.dumps(行)

不输出

您可以使用子句检查字符串是否实际上是json:

import json
line = '<what you think is json>'
try:
    json_line = json.loads(line)
except ValueError:
    print("not a json")
导入json
行=“”
尝试:
json_line=json.loads(line)
除值错误外:
打印(“非json”)

在上面的代码中,最后一行不是有效的JSON。您可以使用这个工具来验证您的JSON是否是有效的JSON。

是的,我试过了。这表示无法检测到json对象。@编码时出错,这是因为您的最后一行不是有效的json。只有这个
{“value”:“30”,“type”:“Tip 3”,“targetModule”:“Target 3”,“configurationGroup”:null,“name”:“Configuration Deneme 3”,“description”:null,“identity”:“Configuration Deneme 3”,“version”:0,“systemId”:3,“active”:true}
最后一行中的段是有效的JSON
import json
line = '<what you think is json>'
try:
    json_line = json.loads(line)
except ValueError:
    print("not a json")