Spaces:
Configuration error
Configuration error
File size: 2,687 Bytes
a7060a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | ---
title: Validate JSON
type: plugins
category: Validation
cat: validation
order: 100
meta_title: Validate JSON
meta_description: Checks that the introduced text is a valid JSON
tier: enterprise
---
<img src="/images/plugins/json-validate-thumb.png" alt="" class="gif-border" style="max-width: 552px !important;" />
!!! note
For information about modifying this plugin or creating your own custom plugins, see [Customize and Build Your Own Plugins](custom).
For general plugin information, see [Plugins for projects](/guide/plugins) and [Plugin FAQ](faq).
## About
This plugin parses the contexts of a `TextArea` field to check for valid JSON. If the JSON is invalid, it shows an error and prevents the annotation from being saved.
This is an example of a "hard" block, meaning that the user must resolve the issue before they can proceed.

## Plugin
```javascript
LSI.on("beforeSaveAnnotation", (store, annotation) => {
const textAreaResult = annotation.results.find(r => r.type === 'textarea' && r.from_name.name === 'answer');
if (textAreaResult) {
try {
JSON.parse(textAreaResult.value.text[0]);
} catch (e) {
Htx.showModal("Invalid JSON format. Please correct the JSON and try again.", "error");
return false;
}
}
return true;
});
```
**Related LSI instance methods:**
* [on(eventName, handler)](custom#LSI-on-eventName-handler)
**Related frontend events:**
* [beforeSaveAnnotation](/guide/frontend_reference#beforeSaveAnnotation)
## Labeling config
```xml
<View>
<View>
<Filter toName="label_rectangles" minlength="0" name="filter"/>
<RectangleLabels name="label_rectangles" toName="image" canRotate="false" smart="true">
<Label value="table" background="Blue"/>
<Label value="cell" background="Red"/>
<Label value="column" background="Green"/>
<Label value="row" background="Purple"/>
</RectangleLabels>
</View>
<View>
<Image name="image" value="$image" />
</View>
<View style=".htx-text { white-space: pre-wrap; }">
<TextArea name="answer" toName="image"
editable="true"
perRegion="true"
required="false"
maxSubmissions="1"
rows="10"
placeholder="Parsed Row JSON"
displayMode="tag"/>
</View>
</View>
```
**Related tags:**
* [View](/tags/view.html)
* [RectangleLabels](/tags/rectanglelabels.html)
* [TextArea](/tags/textarea.html)
* [Labels](/tags/labels.html)
## Sample data
```json
[
{
"image": "https://labelstud.io/demo/Datasets/Image/Object Detection/Image_66.jpg"
}
]
```
|