Hướng dẫn how do i check if a json file is valid in python? - làm cách nào để kiểm tra xem tệp json có hợp lệ trong python không?

Mô-đun JSON tích hợp có thể được sử dụng làm trình xác thực:

import json

def parse[text]:
    try:
        return json.loads[text]
    except ValueError as e:
        print['invalid json: %s' % e]
        return None # or: raise

Bạn có thể làm cho nó hoạt động với các tệp bằng cách sử dụng:

with open[filename] as f:
    return json.load[f]

Thay vì json.loads và bạn cũng có thể bao gồm tên tệp trong thông báo lỗi.

Trên Python 3.3.5, đối với {test: "foo"}, tôi nhận được:

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]

và trên 2.7.6:

invalid json: Expecting property name: line 1 column 2 [char 1]

Điều này là do JSON chính xác là {"test": "foo"}.

Khi xử lý các tệp không hợp lệ, tốt nhất là không xử lý chúng thêm nữa. Bạn có thể xây dựng tệp đã bỏ qua.txt liệt kê các tệp có lỗi, để chúng có thể được kiểm tra và sửa bằng tay.

Nếu có thể, bạn nên kiểm tra trang web/chương trình tạo các tệp JSON không hợp lệ, sửa lỗi đó và sau đó tạo lại tệp JSON. Nếu không, bạn sẽ tiếp tục có các tệp mới là JSON không hợp lệ.

Không điều đó, bạn sẽ cần viết một trình phân tích cú pháp JSON tùy chỉnh để sửa lỗi phổ biến. Với điều đó, bạn nên đặt bản gốc dưới điều khiển nguồn [hoặc lưu trữ], để bạn có thể thấy và kiểm tra sự khác biệt mà công cụ tự động sửa chữa [như kiểm tra tỉnh táo]. Các trường hợp mơ hồ nên được cố định bằng tay.

Đưa ra một chuỗi python, nhiệm vụ là kiểm tra xem chuỗi có phải là đối tượng JSON hợp lệ hay không. Hãy để cố gắng hiểu vấn đề bằng cách sử dụng các ví dụ khác nhau.

Ví dụ 1: Chuỗi JSON không hợp lệ do báo giá không chính xác trong chuỗi Invalid JSON String due to incorrect quotes in Strings

Python3

with open[filename] as f:
    return json.load[f]
0
with open[filename] as f:
    return json.load[f]
1

with open[filename] as f:
    return json.load[f]
2
with open[filename] as f:
    return json.load[f]
3
with open[filename] as f:
    return json.load[f]
4

with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
with open[filename] as f:
    return json.load[f]
7
with open[filename] as f:
    return json.load[f]
8

with open[filename] as f:
    return json.load[f]
9
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
0

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
22
with open[filename] as f:
    return json.load[f]
3
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
4

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1
with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
8
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9

invalid json: Expecting property name: line 1 column 2 [char 1]
0
invalid json: Expecting property name: line 1 column 2 [char 1]
1

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1
with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
invalid json: Expecting property name: line 1 column 2 [char 1]
5
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9

Output:

initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false

Giải thích: Trong ví dụ trên, chuỗi là một JSON không hợp lệ bởi vì, các ký tự chuỗi được đặt trong các trích dẫn đơn [], nhưng theo lược đồ JSON hợp lệ, các chuỗi phải được đặt trong các trích dẫn kép [Hồi]. In the above example, the string is an invalid JSON because, the string characters the enclosed in single quotes [‘], but as per valid JSON schema, strings must be enclosed in double quotes [“].

Ví dụ 2: Ví dụ về chuỗi JSON hợp lệ

Python3

with open[filename] as f:
    return json.load[f]
0
with open[filename] as f:
    return json.load[f]
1

with open[filename] as f:
    return json.load[f]
2
with open[filename] as f:
    return json.load[f]
3
with open[filename] as f:
    return json.load[f]
4

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1
with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false
4
initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false
5

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1
with open[filename] as f:
    return json.load[f]
9
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
0

initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false
9
JSON String: {"name":"John", "age":31, "Salary":2500000}
  Is valid?: True
JSON String: { "Subjects": {"Maths":85.01, "Physics":90}}
  Is valid?: True
JSON String: {"success": true}
  Is valid?: True
0

initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false
9
with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
JSON String: {"name":"John", "age":31, "Salary":2500000}
  Is valid?: True
JSON String: { "Subjects": {"Maths":85.01, "Physics":90}}
  Is valid?: True
JSON String: {"success": true}
  Is valid?: True
4
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9

with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
with open[filename] as f:
    return json.load[f]
7
with open[filename] as f:
    return json.load[f]
8

initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false
9
with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
JSON String: 
{
    "Person": [
        {"name": "P_1", "age": 42},
        {"name": "P_2", "age": 21},
        {"name": "P_3", "age": 36}
    ],
    "location": "1.1.0",
    "Country": "IND"
}

  Is valid?: True
2
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
22
with open[filename] as f:
    return json.load[f]
3
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
4

JSON String: 
{
    "Person": [
        {"name": "P_1", "age": 42},
        {"name": "P_2", "age": 21},
        {"name": "P_3", "age": 36}
    ],
    "location": "1.1.0",
    "Country": "IND"
}

  Is valid?: True
7
JSON String: 
{
    "Person": [
        {"name": "P_1", "age": 42},
        {"name": "P_2", "age": 21},
        {"name": "P_3", "age": 36}
    ],
    "location": "1.1.0",
    "Country": "IND"
}

  Is valid?: True
8
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9

JSON String: 
{
    "Person": [
        {"name": "P_1", "age": 42},
        {"name": "P_2", "age": 21},
        {"name": "P_3", "age": 36}
    ],
    "location": "1.1.0",
    "Country": "IND"
}

  Is valid?: True
7json.loads1
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9

JSON String: 
{
    "Person": [
        {"name": "P_1", "age": 42},
        {"name": "P_2", "age": 21},
        {"name": "P_3", "age": 36}
    ],
    "location": "1.1.0",
    "Country": "IND"
}

  Is valid?: True
7json.loads4
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9

Output:

JSON String: {"name":"John", "age":31, "Salary":2500000}
  Is valid?: True
JSON String: { "Subjects": {"Maths":85.01, "Physics":90}}
  Is valid?: True
JSON String: {"success": true}
  Is valid?: True

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1
with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
8
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9

invalid json: Expecting property name: line 1 column 2 [char 1]
0
invalid json: Expecting property name: line 1 column 2 [char 1]
1json.JSONDecoder able to parse this String.

Python3

with open[filename] as f:
    return json.load[f]
0
with open[filename] as f:
    return json.load[f]
1

with open[filename] as f:
    return json.load[f]
2
with open[filename] as f:
    return json.load[f]
3
with open[filename] as f:
    return json.load[f]
4

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1
with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false
4
initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false
5

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1
with open[filename] as f:
    return json.load[f]
9
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
0

initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false
9
JSON String: {"name":"John", "age":31, "Salary":2500000}
  Is valid?: True
JSON String: { "Subjects": {"Maths":85.01, "Physics":90}}
  Is valid?: True
JSON String: {"success": true}
  Is valid?: True
0

initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false
9
with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
JSON String: {"name":"John", "age":31, "Salary":2500000}
  Is valid?: True
JSON String: { "Subjects": {"Maths":85.01, "Physics":90}}
  Is valid?: True
JSON String: {"success": true}
  Is valid?: True
4
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9

with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
with open[filename] as f:
    return json.load[f]
7
with open[filename] as f:
    return json.load[f]
8

initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false
9
with open[filename] as f:
    return json.load[f]
5
with open[filename] as f:
    return json.load[f]
6
JSON String: 
{
    "Person": [
        {"name": "P_1", "age": 42},
        {"name": "P_2", "age": 21},
        {"name": "P_3", "age": 36}
    ],
    "location": "1.1.0",
    "Country": "IND"
}

  Is valid?: True
2
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
9

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
1
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
22
with open[filename] as f:
    return json.load[f]
3
invalid json: Expecting property name enclosed in double quotes: line 1 column 2 [char 1]
4

with open[filename] as f:
    return json.load[f]
06
with open[filename] as f:
    return json.load[f]
3

with open[filename] as f:
    return json.load[f]
08

Output:

JSON String: 
{
    "Person": [
        {"name": "P_1", "age": 42},
        {"name": "P_2", "age": 21},
        {"name": "P_3", "age": 36}
    ],
    "location": "1.1.0",
    "Country": "IND"
}

  Is valid?: True

Làm thế nào để bạn kiểm tra xem một tệp JSON có hợp lệ hay không?

Cách đơn giản nhất để kiểm tra xem JSON có hợp lệ không phải là tải JSON vào Công việc hoặc Jarray và sau đó sử dụng phương pháp IsValid [JToken, JSchema] với lược đồ JSON.Để nhận được thông báo lỗi xác thực, sử dụng isValid [Jtoken, JSchema, Ilist] hoặc xác nhận [JToken, JSchema, SchemavalidationEventhandler].load the JSON into a JObject or JArray and then use the IsValid[JToken, JSchema] method with the JSON Schema. To get validation error messages use the IsValid[JToken, JSchema, IList ] or Validate[JToken, JSchema, SchemaValidationEventHandler] overloads.

Làm cách nào để xem JSON trong Python?

Đọc tệp JSON trong Python..
Nhập mô -đun JSON ..
Mở tệp bằng tên của tệp JSON witn open [] ..
Mở tệp bằng tên của tệp JSON witn open [] ..
Đọc tệp JSON bằng Load [] và đặt dữ liệu JSON vào một biến ..

Làm cách nào để kiểm tra xem JSON có trống trong Python không?

Làm thế nào để bạn kiểm tra xem một đối tượng trống ?..
Chuyển đối tượng cho đối tượng.Phương thức khóa để nhận một mảng các phím của đối tượng ..
Truy cập thuộc tính độ dài trên mảng ..
Kiểm tra xem độ dài của các phím có bằng 0 không, nếu có, thì đối tượng trống ..

Bài Viết Liên Quan

Chủ Đề