Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find by key get value and replace by second json value in nested json object

I have an json object(firstObj) It can be nested and I have an second object containing key/value pair. I want to replace the second object's value by first one by matching value and do it's operation.

let firstObj = {
    amount_money: {
       amount: {
           mapped_field: 'payment_amt',
           operation: '/10'
       },
       currency: {
          mapped_field: 'payment_cur',
          operation: null
       }
    },
   source_id: {
          mapped_field: 'request_id',
          operation: null
   },
  ship: [ 
            { mapped_field: 'ship_country[0]', operation: null },
            { mapped_field: 'ship_country[1]', operation: null } 
        ]
 };
       

my second object

let secondObj = {
     payment_amt: 100,
     payment_cur: 'USD',
     request_id: '123ASD',
     ship_country: [ 
                     { code: 'USA', Title: 'America' }, 
                     { code: 'UK', Title: 'England' } 
                   ] 
    };

I want something like this

{
  amount_money: {
     amount: 10
     currency: 'USD'
  },
  source_id: '123ASD',
  ship: [ {America: 'USA'}, {England: 'UK'}]
}

Really appreciate your kind help, Thank you!

like image 235
supunbatagoda Avatar asked Mar 19 '26 19:03

supunbatagoda


1 Answers

Update

If you don't know how many nested nodes are there. There is a more overall solution using recursion.

Here is the solution:

const firstObj = {
  source_name: {
    mapped_field: 'request_name',
    operation: null,
  },
  amount_money: {
    amount: {
      mapped_field: 'payment_amt',
      operation: '/10',
    },
    currency: {
      mapped_field: 'payment_cur',
      operation: null,
    },
  },
  source_id: {
    mapped_field: 'request_id',
    operation: null,
  },
  nested: {
    nested: {
      nested: {
        nested: {
          mapped_field: 'mapping_nested',
          operation: null,
        },
      },
    },
  },
};

let secondObj = {
  payment_amt: 100,
  payment_cur: 'USD',
  request_id: '123ASD',
  request_name: 'Dollar',
  mapping_nested: 'Hello',
};

const procedure = (firstObj, parentObj = {}, nestedObj = {}) => {
  for (const [key, value] of Object.entries(firstObj)) {
    if (value.hasOwnProperty('mapped_field') && value.hasOwnProperty('operation')) {
      nestedObj[key] = value.operation
        ? eval(secondObj[value.mapped_field] + value.operation)
        : secondObj[value.mapped_field];
    } else {
      nestedObj[key] = {};
      procedure(value, parentObj, nestedObj[key]);
    }
  }
  return (parentObj = { ...nestedObj });
};

const result = procedure(firstObj);
console.log(JSON.stringify(result));
// {"source_name":"Dollar","amount_money":{"amount":10,"currency":"USD"},"source_id":"123ASD","nested":{"nested":{"nested":{"nested":"Hello"}}}}

Old answer

I don't know how many nested in your firstObj. But this code below can solve the example which you give.

I replace the second object's value by first one by matching value.

If operation not equal null, use eval() function evaluates JavaScript code represented as a string.

const result = {};
for (const [parentKey, parentValue] of Object.entries(firstObj)) {
  result[parentKey] = {};
  for (const [childKey, childValue] of Object.entries(parentValue)) {
    result[parentKey][childKey] = childValue.operation
      ? eval(secondObj[childValue.mapped_field] + childValue.operation)
      : secondObj[childValue.mapped_field];
  }
}
console.log(result); //{ amount_money: { amount: 10, currency: 'USD' } }

Note: If firstObj have only one key like amount_money, you can make your code precise by using only one loop.

like image 137
Duy Quoc Avatar answered Mar 22 '26 07:03

Duy Quoc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!