From 951c7f0f489b6b491caa3c0ed8a8b2199bea89a7 Mon Sep 17 00:00:00 2001 From: Xiaoran Zhou Date: Tue, 22 Oct 2024 12:39:39 +0200 Subject: [PATCH 1/4] adding jsonata_validate prototype --- mars-cli/jsonata_validate.py | 77 ++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 mars-cli/jsonata_validate.py diff --git a/mars-cli/jsonata_validate.py b/mars-cli/jsonata_validate.py new file mode 100644 index 0000000..c1c35d9 --- /dev/null +++ b/mars-cli/jsonata_validate.py @@ -0,0 +1,77 @@ +import jsonata +import requests +import csv +import json +import argparse + +def main(filepath='../test-data/biosamples-original-isa.json', + table_url='https://docs.google.com/spreadsheets/d/e/2PACX-1vQvgQoUByiJgGcJ4jtD8bG9AyQrh4TYVQE8aq7AqJRxLdfyLFATKspu_vkyqbVsTyEnNIBHqWtpgV6X/pub?gid=0&single=true&output=csv'): + """ + Main function to validate JSON data using a JSONata expression extracted from a CSV file. + + Parameters: + filepath (str): The path to the JSON file to be validated. + table_url (str): The URL of the CSV file hosted online that contains JSONata expressions. + + Returns: + None: This function prints the index and value of invalid JSONata expressions. + """ + + try: + # Fetch CSV data from the provided URL + res = requests.get(table_url) + res.raise_for_status() # Check for HTTP request errors + except requests.exceptions.RequestException as e: + print(f"Error fetching the table from the URL: {e}") + return + + try: + # Parse the fetched CSV data into a list of rows + table = csv.reader(res.text.split('\n')) + jsonata_list = list(table) + except Exception as e: + print(f"Error reading CSV data: {e}") + return + + try: + # Open and load the JSON file + with open(filepath, 'r') as file: + data = json.load(file) + except FileNotFoundError: + print(f"Error: The file '{filepath}' was not found.") + return + except json.JSONDecodeError as e: + print(f"Error parsing JSON data: {e}") + return + + # Loop through each row in the JSONata list starting from index 3 + for index, ele in enumerate(jsonata_list): + if len(ele) > 6 and ele[6] != "" and index > 2: # Check if there is a valid JSONata expression + try: + # Evaluate the JSONata expression + expr = jsonata.Jsonata("'"+ele[6]) # Correcting the JSONata expression input + result = expr.evaluate(data) + + if result == False: # If the evaluation fails, print details + print(f"Validation failed at row {index}:") + print(f"Sample ID: {ele[0]}") + print(f"Expression: {ele[6]}") + print(f"Result: {result}") + except Exception as e: + print(f"Error evaluating JSONata at row {index}: {e}") + continue + +if __name__ == '__main__': + # help(main) + # Argument parser for command line inputs + parser = argparse.ArgumentParser(description='Validate JSON data using JSONata expressions from a CSV file.') + + # Define command-line arguments + parser.add_argument('--filepath', type=str, required=True, help='Path to the JSON file to be validated.') + parser.add_argument('--table_url', type=str, required=False, help='URL of the CSV file containing JSONata expressions.', default='https://docs.google.com/spreadsheets/d/e/2PACX-1vQvgQoUByiJgGcJ4jtD8bG9AyQrh4TYVQE8aq7AqJRxLdfyLFATKspu_vkyqbVsTyEnNIBHqWtpgV6X/pub?gid=0&single=true&output=csv') + + # Parse the arguments + args = parser.parse_args() + + # Call the main function with arguments from the command line + main(filepath=args.filepath, table_url=args.table_url) \ No newline at end of file From e1a71cac0c802d03854cf8d78301f423d9ce978f Mon Sep 17 00:00:00 2001 From: Xiaoran Zhou Date: Thu, 7 Nov 2024 16:42:17 +0100 Subject: [PATCH 2/4] remove cross origin --- .../biosamples/controller/BioSampleSubmissionController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/controller/BioSampleSubmissionController.java b/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/controller/BioSampleSubmissionController.java index ed3fe0a..59797b2 100644 --- a/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/controller/BioSampleSubmissionController.java +++ b/repository-services/isajson-biosamples/src/main/java/com/elixir/biohackaton/ISAToSRA/biosamples/controller/BioSampleSubmissionController.java @@ -37,7 +37,7 @@ public class BioSampleSubmissionController { @ApiResponse(responseCode = "408", description = "Request Timeout"), @ApiResponse(responseCode = "415", description = "Unsupported media type") }) - @CrossOrigin(origins = "http://localhost:8000") +// @CrossOrigin(origins = "http://localhost:8000") @PostMapping( value = "/submit", consumes = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE}) From 15a10465d2ff5a0f4fd85b93517f683465d8ec32 Mon Sep 17 00:00:00 2001 From: Xiaoran Zhou Date: Fri, 8 Nov 2024 09:26:36 +0100 Subject: [PATCH 3/4] update --- mars-cli/{ => mars_lib}/jsonata_validate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename mars-cli/{ => mars_lib}/jsonata_validate.py (96%) diff --git a/mars-cli/jsonata_validate.py b/mars-cli/mars_lib/jsonata_validate.py similarity index 96% rename from mars-cli/jsonata_validate.py rename to mars-cli/mars_lib/jsonata_validate.py index c1c35d9..57067b2 100644 --- a/mars-cli/jsonata_validate.py +++ b/mars-cli/mars_lib/jsonata_validate.py @@ -67,7 +67,7 @@ def main(filepath='../test-data/biosamples-original-isa.json', parser = argparse.ArgumentParser(description='Validate JSON data using JSONata expressions from a CSV file.') # Define command-line arguments - parser.add_argument('--filepath', type=str, required=True, help='Path to the JSON file to be validated.') + parser.add_argument('filepath', type=str, help='Path to the JSON file to be validated.') parser.add_argument('--table_url', type=str, required=False, help='URL of the CSV file containing JSONata expressions.', default='https://docs.google.com/spreadsheets/d/e/2PACX-1vQvgQoUByiJgGcJ4jtD8bG9AyQrh4TYVQE8aq7AqJRxLdfyLFATKspu_vkyqbVsTyEnNIBHqWtpgV6X/pub?gid=0&single=true&output=csv') # Parse the arguments From f96ca4d1b49b4450c31c806233f6e4fd8a64b16e Mon Sep 17 00:00:00 2001 From: Xiaoran Zhou Date: Fri, 8 Nov 2024 09:51:33 +0100 Subject: [PATCH 4/4] add jsonata-python to requirements.txt --- mars-cli/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/mars-cli/requirements.txt b/mars-cli/requirements.txt index 2c37368..2f05f5d 100644 --- a/mars-cli/requirements.txt +++ b/mars-cli/requirements.txt @@ -4,3 +4,4 @@ keyring pydantic click retry +jsonata-python