This is how I would do it, not using the ai itself to do the rounding.
- Parse the JSON data** to access the relevant fields.
- Search for the number** that matches the specific format you need (for example, invoice numbers, transaction IDs, etc.).
- Return or perform an action based on whether the number is found and correctly formatted.
Let’s assume your invoice data looks something like this in JSON:
json
Copy code
{
"invoices": [
{
"invoice_number": "INV-12345",
"date": "2024-10-24",
"total": 1050.00,
"items": [
{
"description": "Item 1",
"quantity": 10,
"price": 50
}
]
},
{
"invoice_number": "INV-67890",
"date": "2024-10-24",
"total": 500.00,
"items": [
{
"description": "Item 2",
"quantity": 5,
"price": 100
}
]
}
]
}
Let’s say the format you are checking for is INV-XXXXX
(where XXXXX
is a number).
Sample Python function:
python
import re
import json
# Example function to check for a specific number format in the JSON output
def check_invoice_number(json_data, number_format="INV-\d{5}"):
# Parse the JSON data (if it's a JSON string)
if isinstance(json_data, str):
data = json.loads(json_data)
else:
data = json_data
# Compile the regular expression for the specific number format
pattern = re.compile(number_format)
# Iterate over the invoices and check the format of the invoice numbers
for invoice in data.get("invoices", []):
invoice_number = invoice.get("invoice_number", "")
if pattern.match(invoice_number):
print(f"Invoice number {invoice_number} is valid.")
else:
print(f"Invoice number {invoice_number} is invalid.")
# Example usage
json_data = {
"invoices": [
{
"invoice_number": "INV-12345",
"date": "2024-10-24",
"total": 1050.00,
"items": [
{
"description": "Item 1",
"quantity": 10,
"price": 50
}
]
},
{
"invoice_number": "INV-ABC12",
"date": "2024-10-24",
"total": 500.00,
"items": [
{
"description": "Item 2",
"quantity": 5,
"price": 100
}
]
}
]
}
# Call the function to check invoice numbers
check_invoice_number(json_data)
- Regex Pattern:
INV-\d{5}
checks for the format where “INV-” is followed by exactly 5 digits.
- Pattern Matching: For each invoice in the JSON, the function checks whether the
invoice_number
matches the pattern.
- JSON Handling: The function accepts either a JSON string or a parsed JSON object.
- Action: It prints whether each invoice number is valid or not, but you can customize this (e.g., raise an error or return a list of valid/invalid numbers).
Output:
Invoice number INV-12345 is valid.
Invoice number INV-ABC12 is invalid.
This approach allows you to search for numbers that follow a specific format and handle your OCR data systematically. You can easily adjust the regex pattern for other formats if needed.
The OCR data you would have to use your JSON formatting or Ai to format it for you. than build in the logic to pass each format through looking for the part that you want to check and run a rounding logic depending on your needs. you could than use validation Ai or other to check if its now valid.
In the previous I thought you were just looking for one that were valid and not valid only to flag them, so difference would be adding logic in the middle of your processing to do the rounding. Ai itself is not the best at math but it can call functions you create with understanding.
Hope that makes more sense. its more about breaking the data down into a format to process.
This is just to show a method, not your exact but you should get the idea from it.
Now if your numbers are not always 5 digits you would have to build a more complex handler.