I am use microsoft.clearscript.v8 in asp.net core mvc, in the path of inputpath :{{(}}&Account.Order..Product..Quantity&{{}}&Account.Order..Product..Price&{{)}} is not working regex, i want regex {{[^{}]+}} remove and the two paths are get and multiplying with each others and Print in the Outputpath : Account.Order1..Product.*.Total path, this is my homecontroller.cs code.
var additionalJsons = @"
{
""Account"": {
""AccountName"": ""Firefly"",
""Order"": [
{
""OrderID"": ""order103"",
""Product"": [
{
""ProductName"": ""Bowler Hat"",
""ProductID"": 858383,
""Price"": 34,
""Quantity"": 2
},
{
""ProductName"": ""Trilby hat"",
""ProductID"": 858236,
""SKU"": ""0406634348"",
""Price"": 21,
""Quantity"": 1
}
]
},
{
""OrderID"": ""order104"",
""Product"": [
{
""ProductName"": ""Bowler Hat"",
""ProductID"": 858383,
""SKU"": ""040657863"",
""Price"": 34,
""Quantity"": 4
},
{
""ProductID"": 345664,
""SKU"": ""0406654603"",
""ProductName"": ""Cloak"",
""Price"": 107,
""Quantity"": 1
}
]
}
]
}
}";
using (var engine = new V8ScriptEngine())
{
// Load the additional JSON data and rules into the script engine
engine.Execute($"var additionalJson = {additionalJsons};");
engine.Execute("var final = [];");
engine.Execute(@"
class Rule {
constructor(inputPath, outputPath, type, value) {
this.inputPath = inputPath;
this.outputPath = outputPath;
this.type = type;
this.value = value;
}
}
// Create a list of rules with type information and target paths
var rules = [
new Rule('Account.Order.*.Product.*.ProductName', 'Account.Order1.*.Product.*.ProductN', 'ChangeFieldName'),
new Rule('Account.Order.*.Product.*.ProductID', 'Account.Order1.*.Product.*.NewProductID', 'ChangeFieldName'),
new Rule('Account.Order.0.Product.0.Price', 'Account.Order1.0.Product.0.Price', 'ChangeValueName', 50),
// Add your new rule here
];
// Function to apply rules to modify field names for a given object
function applyRules(targetObj, rules) {
var modifiedData = {};
rules.forEach(rule => {
var inputPaths = getMatchingPaths(rule.inputPath);
var outputPaths = getMatchingPaths(rule.outputPath);
if (inputPaths.length === outputPaths.length) {
for (var i = 0; i < inputPaths.length; i++) {
var inputValue = getValueByPath(targetObj, inputPaths[i]);
if (inputValue !== undefined) {
if (rule.type === 'ChangeFieldName') {
setValueByPath(modifiedData, outputPaths[i], inputValue);
} else if (rule.type === 'ChangeValueName') {
setValueByPath(modifiedData, outputPaths[i], rule.value);
}
}
}
}
});
return modifiedData;
}
// Function to get matching paths in an array based on a given path pattern
function getMatchingPaths(pathPattern) {
var paths = [];
for (var i = 0; i < 2; i++) {
for (var j = 0; j < 2; j++) {
paths.push(pathPattern.replace('*', i).replace('*', j));
}
}
return paths;
}
// Function to get value from an object based on a given path
function getValueByPath(obj, path) {
var keys = path.split('.');
var value = obj;
for (var key of keys) {
if (value[key] !== undefined) {
value = value[key];
} else {
return undefined;
}
}
return value;
}
// Function to set value in an object based on a given path
function setValueByPath(obj, path, value) {
var keys = path.split('.');
var lastKey = keys.pop();
var destination = obj;
for (var key of keys) {
destination[key] = destination[key] || {};
destination = destination[key];
}
destination[lastKey] = value;
}
// Call the function to apply rules to modify field names
var final = [applyRules(additionalJson, 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": {
"Order1": {
"0": {
"Product": {
"0": {
"NewProductID": 858383,
"Price": 50
},
"1": {
"ProductN": "Trilby hat",
"NewProductID": 858236
}
}
},
"1": {
"Product": {
"0": {
"NewProductID": 858383
},
"1": {
"NewProductID": 345664
}
}
}
}
}
}
]
but i want add rule with full path of jsondata like this inputpath :{{(}}&Account.Order..Product..Quantity&{{}}&Account.Order..Product.*.Price&{{)}} and only order103 price change, i want this Output :
[
{
"Account": {
"Order1": {
"0": {
"Product": {
"0": {
"NewProductID": 858383,
"Price": 50,
"Total": 68
},
"1": {
"ProductN": "Trilby hat",
"NewProductID": 858236,
"Total": 21
}
}
},
"1": {
"Product": {
"0": {
"NewProductID": 858383,
"Total": 136
},
"1": {
"NewProductID": 345664,
"Total": 107
}
}
}
}
}
}
]