|
2 | 2 |
|
3 | 3 | `json.invalid`
|
4 | 4 |
|
5 |
| -Something in the json structure is considered an invalid json structure. Usually around the reported error. |
| 5 | +When you encounter this error, it indicates that there is something wrong with the JSON structure. Below are some common mistakes that lead to invalid JSON and how to fix them. |
| 6 | + |
| 7 | +Common JSON Errors |
| 8 | + |
| 9 | +## 1. Missing Quotes Around Strings |
| 10 | + |
| 11 | +JSON requires that all strings are enclosed in double quotes. Missing quotes will lead to a parsing error. |
| 12 | + |
| 13 | +Example of invalid JSON: |
| 14 | + |
| 15 | +```jsonc |
| 16 | +{ |
| 17 | + name: John Doe |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +Fix: |
| 22 | + |
| 23 | +```jsonc |
| 24 | +{ |
| 25 | + "name": "John Doe" |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## 2. Missing commas |
| 30 | + |
| 31 | +Trailing commas are not allowed in JSON. Having a comma after the last key-value pair in an object or the last element in an array will cause an error. |
| 32 | + |
| 33 | +Example of invalid JSON: |
| 34 | + |
| 35 | +```jsonc |
| 36 | +{ |
| 37 | + "name": "John Doe" |
| 38 | + "age": 30, |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +Fix: |
| 43 | + |
| 44 | +```jsonc |
| 45 | +{ |
| 46 | + "name": "John Doe", |
| 47 | + "age": 30 |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## 3. Trailing Commas |
| 52 | + |
| 53 | +Trailing commas are not allowed in JSON. Having a comma after the last key-value pair in an object or the last element in an array will cause an error. |
| 54 | + |
| 55 | +Example of invalid JSON: |
| 56 | + |
| 57 | +```jsonc |
| 58 | +{ |
| 59 | + "name": "John Doe", |
| 60 | + "age": 30, |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Fix: |
| 65 | + |
| 66 | +```jsonc |
| 67 | +{ |
| 68 | + "name": "John Doe", |
| 69 | + "age": 30 |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## 4. Using Single Quotes |
| 74 | + |
| 75 | +JSON does not support single quotes for strings. Strings must be enclosed in double quotes. |
| 76 | + |
| 77 | +Example of invalid JSON: |
| 78 | + |
| 79 | +```jsonc |
| 80 | +{ |
| 81 | + 'name': 'John Doe' |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +Fix: |
| 86 | + |
| 87 | +```jsonc |
| 88 | +{ |
| 89 | + "name": "John Doe" |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## 5. Unescaped Characters |
| 94 | + |
| 95 | +Certain characters in strings need to be escaped, such as backslashes, double quotes within strings, and control characters like newlines. |
| 96 | + |
| 97 | +Example of invalid JSON: |
| 98 | + |
| 99 | +```jsonc |
| 100 | +{ |
| 101 | + "text": "He said, "Hello, World!"" |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +Fix: |
| 106 | + |
| 107 | +```jsonc |
| 108 | +{ |
| 109 | + "text": "He said, \"Hello, World!\"" |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## 6. Incorrect Data Types |
| 114 | + |
| 115 | +JSON only supports specific data types: `string`, `number`, `object`, `array`, `true`, `false`, and `null`. Using unsupported data types like undefined or functions will result in an error. |
| 116 | + |
| 117 | +Example of invalid JSON: |
| 118 | + |
| 119 | +```jsonc |
| 120 | +{ |
| 121 | + "value": undefined |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +Fix: |
| 126 | + |
| 127 | +```jsonc |
| 128 | +{ |
| 129 | + "value": null |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +## 7. Nested Errors |
| 134 | + |
| 135 | +Errors can also occur in nested structures if any of the above issues are present at any level of the JSON. |
| 136 | + |
| 137 | +Example of invalid JSON: |
| 138 | + |
| 139 | +```jsonc |
| 140 | +{ |
| 141 | + "user": { |
| 142 | + "name": "John Doe", |
| 143 | + "details": { "age": 30, "gender": "male", } // <-- trailing comma |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +Fix: |
| 149 | + |
| 150 | +```jsonc |
| 151 | +{ |
| 152 | + "user": { |
| 153 | + "name": "John Doe", |
| 154 | + "details": { "age": 30, "gender": "male" } |
| 155 | + } |
| 156 | +} |
| 157 | +``` |
0 commit comments