Python / json-repair
===
###### tags: `Python`
###### tags: `Python`, `json-repair`, `json-fix`
<br>
[TOC]
<br>
:::danger
### :-1: 不建議使用,尚未達堪用程度
:::
<br>
## json_repair
https://github.com/mangiucugna/json_repair
### Install
```
pip install json-repair
```
### code
```python=
from json_repair import repair_json
good_json_string = repair_json(bad_json_string)
# If the string was super broken this will return an empty string
```
<br>
## bad
### case1
```json=
from json_repair import repair_json
import json
bad_json_string = '"`Processor` is a string constant, `\\INT1` and `\\CPU0` are references to identifiers."\n'
repair_json(bad_json_string)
```
- 執行結果:
```
'""'
```
<br>
### case2
```python=
d = {'id': 'chat-02f2ad52a7774a4089da42ae31f6be88', 'choices': [{'finish_reason': 'stop', 'index': 0, 'logprobs': None, 'message': {'content': '{\n "dialogue": [\n {"role": "student", "value": "What is the purpose of the `PinFunction` macro?"},\n {"role": "teacher", "value": "The `PinFunction` macro is designed for configuring individual pins in a system. It allows for detailed control over the configuration, such as shared or exclusive use, pin pull configuration, function number, resource source, index, usage, descriptor name, and vendor-specific data."},\n {"role": "student", "value": "Can you explain the parameters of the `PinFunction` macro?"},\n {"role": "teacher", "value": "Sure! The `PinFunction` macro has the following parameters: `Shared/Exclusive`, `PinPullConfiguration`, `FunctionNumber`, `ResourceSource`, `ResourceSourceIndex`, `ResourceUsage`, `DescriptorName`, and `VendorData`. Additionally, there is a list of pins indicated by `{Pin List}` that can be included or referred to within the macro definition."},\n {"role": "student", "value": "How would you use this macro in a practical scenario?"},\n {"role": "teacher", "value": "In a practical scenario, you might use the `PinFunction` macro to configure pins for a device or system. For example, you could use it to configure a GPIO pin as an input with a specific resource source and usage. An example usage could be: `PinFunction (Shared, None, 0, "GPIO", 2, "Input", "GPIO_PIN0", "123456")`."},\n {"role": "student", "value": "What does the `{Pin List}` signify in the macro definition?"},\n {"role": "teacher", "value": "The `{Pin List}` signifies that there might be additional parameters or arguments that follow the same structure as the rest of the parameters. This could include specifying the pins individually or using a list of pins as part of the macro configuration."}\n ]\n}', 'role': 'assistant', 'function_call': None, 'tool_calls': []}, 'stop_reason': None}], 'created': 1726718412, 'model': 'qwen2-vl-7b', 'object': 'chat.completion', 'service_tier': None, 'system_fingerprint': None, 'usage': {'completion_tokens': 418, 'prompt_tokens': 1008, 'total_tokens': 1426}, 'prompt_logprobs': None}
print(d['choices'][0]['message']['content'])
print('---')
print(json.loads(repair_json(d['choices'][0]['message']['content'])))
```
- 執行結果:
