No compiled code to run error CS1001

No compiled code to run
error CS1001: Identifier expected At line 1
error CS1001: Identifier expected At line 2
error CS1001: Identifier expected At line 3
error CS0106: The modifier ‘public’ is not valid for this item At line 5
error CS0118: ‘Newtonsoft.Json.Linq’ is a namespace but is used like a type At line 1
error CS0210: You must provide an initializer in a fixed or using statement declaration At line 1
error CS0118: ‘System.Collections.Generic’ is a namespace but is used like a type At line 2
error CS0210: You must provide an initializer in a fixed or using statement declaration At line 2
error CS0118: ‘System.Text.RegularExpressions’ is a namespace but is used like a type At line 3
error CS0210: You must provide an initializer in a fixed or using statement declaration At line 3

my code

using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public void ValidateJsonArray(string jsonString)
{
// Parse the JSON array
JArray jsonArray = JArray.Parse(jsonString);

List<string> validationErrors = new List<string>();

// Iterate over each JSON object in the array
foreach (JObject jsonData in jsonArray)
{
    // Define allowed categories
    HashSet<string> allowedCategories = new HashSet<string> { "handguns", "rifles", "shotguns", "muzzleloaders", "black_powder_firearms" };

    // Validate category
    string category = (string)jsonData["data"]["category"];
    if (!allowedCategories.Contains(category))
    {
        validationErrors.Add("Invalid category.");
    }

    // Validate UPC (string with numbers only)
    string upc = (string)jsonData["data"]["upc"];
    if (!Regex.IsMatch(upc, @"^\d+$"))
    {
        validationErrors.Add("UPC must be a string with numbers only.");
    }

    // Validate weight, barrel_length, overall_length (must be numbers)
    if (!double.TryParse((string)jsonData["data"]["weight"], out _))
    {
        validationErrors.Add("Weight must be a number.");
    }
    if (!double.TryParse((string)jsonData["data"]["barrel_length"], out _))
    {
        validationErrors.Add("Barrel length must be a number.");
    }
    if (!double.TryParse((string)jsonData["data"]["overall_length"], out _))
    {
        validationErrors.Add("Overall length must be a number.");
    }

    // Validate capacity (object with required fields)
    JObject capacity = (JObject)jsonData["data"]["capacity"];
    if (capacity == null || !capacity.ContainsKey("magazine") || !capacity.ContainsKey("chamber"))
    {
        validationErrors.Add("Capacity must have 'magazine' and 'chamber' fields.");
    }
    if (!int.TryParse((string)capacity["magazine"], out _) || !int.TryParse((string)capacity["chamber"], out _))
    {
        validationErrors.Add("Magazine and Chamber must be numbers.");
    }

    // Validate other fields
    string[] stringFields = { "color", "gauge", "frame_size", "stock_material", "stock_type" };
    foreach (var field in stringFields)
    {
        string value = (string)jsonData["data"][field];
        if (string.IsNullOrWhiteSpace(value))
        {
            validationErrors.Add($"{field} must be a string.");
        }
    }
    if (!int.TryParse((string)jsonData["data"]["magazines_included"], out _))
    {
        validationErrors.Add("Magazines included must be an integer.");
    }

    // Validate raw_data (structured HTML table)
    string rawData = (string)jsonData["data"]["raw_data"];
    if (!rawData.StartsWith("<table>"))
    {
        validationErrors.Add("Raw data must start with a <table> tag.");
    }

    // Validate state_compliance (object with boolean values)
    JObject stateCompliance = (JObject)jsonData["data"]["state_compliance"];
    if (stateCompliance == null)
    {
        validationErrors.Add("State compliance must be an object with boolean values.");
    }

    // Validate images (ids array must be integers)
    JObject images = (JObject)jsonData["data"]["images"];
    if (images != null)
    {
        JArray ids = (JArray)images["ids"];
        if (ids != null)
        {
            foreach (var id in ids)
            {
                if (!int.TryParse(id.ToString(), out _))
                {
                    validationErrors.Add("Image IDs must be integers.");
                }
            }
        }
    }
}

// Output results
if (validationErrors.Count > 0)
{
    foreach (var error in validationErrors)
    {
        Console.WriteLine(error); // Replace with Log Message activity in UiPath
    }
}
else
{
    Console.WriteLine("All JSON objects are valid."); // Replace with Log Message activity in UiPath
}

}

Hi,

Do you use InvokeCode activity? If so,the following may help you.

Regards,

thank you so much for your reply