How to use JavaScript Object Path in the microsoft.clearscript.v8

85 views Asked by At

I am use microsoft.clearscript.v8 in asp .net core mvc, in the path of Account.Order[0].Product[0].Price is not working, this is my homecontroller.cs code

        var additionalJsons = @"
        {
            ""Account"": {
                ""AccountName"": ""Firefly"",
                ""Order"": [
                    {
                        ""OrderID"": ""order103"",
                        ""Product"": [
                            {
                                ""ProductName"": ""Bowler Hat"",
                                ""ProductID"": 858383,
                                ""Price"": 34.45,
                                ""Quantity"": 2
                            },
                            {
                                ""ProductName"": ""Trilby hat"",
                                ""ProductID"": 858236,
                                ""SKU"": ""0406634348"",
                                ""Price"": 21.67,
                                ""Quantity"": 1
                            }
                        ]
                    },
                    {
                        ""OrderID"": ""order104"",
                        ""Product"": [
                            {
                                ""ProductName"": ""Bowler Hat"",
                                ""ProductID"": 858383,
                                ""SKU"": ""040657863"",
                                ""Price"": 34.45,
                                ""Quantity"": 4
                            },
                            {
                                ""ProductID"": 345664,
                            ""SKU"": ""0406654603"",
                            ""ProductName"": ""Cloak"",
                            ""Price"": 107.99,
                            ""Quantity"": 1
                        }
                        ]
                    }
                ]
            }
        }";


        using (var engine = new V8ScriptEngine())
        {
            // Load the additional JSON data and rules into the script engine
            engine.Execute($"var additionalJson = {additionalJsons};");

            // Create a new array to store the modified JSON data
            engine.Execute("var final = [];");

            // Execute code to modify field names and values using rules and store in 'final'
            engine.Execute(@"
                class Rule {
                    constructor(original, changed, type) {
                        this.original = original;
                        this.changed = changed;
                        this.type = type;
                    }
                }

                // Create a list of rules with type information
                var rules = [
                    new Rule('ProductName', 'ProductN', 'ChangeFieldName'),
                    new Rule('AccountName', 'NewAccountName', 'ChangeFieldName'),
                    new Rule('Price', 30, 'ChangeValue'), // New rule for changing Price to 30 in order103
                   // Add more rules as needed
                ];
            ");

            // Create a new array to store the modified JSON data
            engine.Execute("var final = [];");

            // Execute code to modify field names and values using rules and store in 'final'
            engine.Execute(@"
                  function deepCopy(obj) {
                    if (obj === null || typeof obj !== 'object') {
                        return obj;
                    }

                    if (Array.isArray(obj)) {
                        var copyArray = [];
                        for (var i = 0; i < obj.length; i++) {
                            copyArray[i] = deepCopy(obj[i]);
                        }
                        return copyArray;
                    }

                    var copyObj = {};
                    for (var key in obj) {
                        if (obj.hasOwnProperty(key)) {
                            copyObj[key] = deepCopy(obj[key]);
                        }
                    }
                    return copyObj;
                }

                // Call the function to create a deep copy of the additionalJson object
                var modifiedJson = deepCopy(additionalJson);

                // Create a new array to store the modified JSON data
                var final = [];

                // Modify the deep copy to change field names and values based on type and store in 'final'
                function modifyFieldNamesAndValues(obj, rules) {
                    if (Array.isArray(obj)) {
                        var copyArray = [];
                        for (var i = 0; i < obj.length; i++) {
                            copyArray[i] = modifyFieldNamesAndValues(obj[i], rules);
                        }
                        return copyArray;
                    }

                    var currentObj = {};
                    for (var key in obj) {
                        if (obj.hasOwnProperty(key)) {
                            if (typeof obj[key] === 'object' && obj[key] !== null) {
                                // Recursively call the function for nested objects
                                currentObj[key] = modifyFieldNamesAndValues(obj[key], rules);
                            } else {
                                // Apply rules to modify field names and values based on type
                                var rule = rules.find(r => r.original === key || r.original === obj[key]);
                                if (rule) {
                                    if (rule.type === 'ChangeFieldName') {
                                        currentObj[rule.changed] = obj[key];
                                    } else if (rule.type === 'ChangeValue') {
                                        currentObj[key] = rule.changed;
                                    }
                                }
                            }
                        }
                    }

                    return currentObj;
                }

                // Call the function to apply rules to modify field names and values
                final.push(modifyFieldNamesAndValues(modifiedJson, rules));

                // Convert the final array to JSON
                var jsonString = JSON.stringify(final, null, 2);
                jsonString;
            ");


            // Display the final modified JSON
            var jsonStrings = engine.Script.jsonString.ToString();
            Console.WriteLine(jsonStrings);

in this code the Output is :

[
     {
"Account": {
  "NewAccountName": "Firefly",
  "Order": [
    {
      "Product": [
        {
          "ProductN": "Bowler Hat",
          "Price": 30
        },
        {
          "ProductN": "Trilby hat",
          "Price": 30
        }
      ]
    },
    {
      "Product": [
        {
          "ProductN": "Bowler Hat",
          "Price": 30
        },
        {
          "ProductN": "Cloak",
          "Price": 30
        }
      ]
    }
  ]
}}]

but i want add rule with full path of jsondata like this Account.Order[0].Product[0].Price and only order103 price change, i want this Output :

 [
 {
"Account": {
  "NewAccountName": "Firefly",
  "Order": [
    {
      "Product": [
        {
          "ProductN": "Bowler Hat",
          "Price": 30
        },
        {
          "ProductN": "Trilby hat",
        }
      ]
    },
    {
      "Product": [
        {
          "ProductN": "Bowler Hat",
        },
        {
          "ProductN": "Cloak",
        }
      ]
    }
  ]
}}]
0

There are 0 answers