0

I'm trying to construct a function which will replace the value based on the path user given as input argument. I have a complex JSON with some duplicated key and user will give path of the element and based on that path It needs to replace the value of the particular key (which is possibly duplicated):

SampleJSON.json:

  {
        "product": "delivery",
        "Merchant": {
            "payment_method": "GPay"
        },
        "transactions": [
            {
                "amount": {
                    "total": "100",
                    "currency": "USD",
                    "details": {
                        "subtotal": "100",
                        "tax": "1",
                        "insurance": "0.1"
                    }
                },
                "description": "The payment transaction description",
                "custom": "JACKSON DISPUTERS SYSTEMS",
                "invoicenumber": "",
                "descriptor": "null",
                "itemlist": {
                    "items": [
                        {
                            "name": "COMPUTER",
                            "description": "BLACK",
                            "quantity": "1",
                            "price": "100",
                            "tax": "1",
                            "currency": "USD"
                        }
                    ],
                    "shippingaddress": {
                        "recipient_name": "JACKSON DISPUTERS SYSTEMS",
                        "line1": "XXXX",
                        "line2": "XXXX",
                        "city": "XXXX",
                        "country_code": "XX",
                        "postal_code": "XXXX",
                        "phone": "XXXXXX",
                        "state": "XXXXXX"
                    }
                }
            }
        ],
        "note_to_payer": "Contact us for any questions on your order."
        }

User Input:

arg1: product.transactions.currency,
arg2: EUR

I just need to traverse through the above path (arg1) and replace the value of currency as AUD (arg2).

If you noticed the currency element is available in two different places in the given JSON file but I just want to replace the currency which is under the transactions array that mentioned in the arg1, and not the other one.

I just created the below code so far and It will traverse through each and every element in the given JSON but not sure how to replace the value for the given key (arg1).

  const arg1 = "product.transactions.currency";
  const condition = arg1.toString().split(".");
  const arg2 = "EUR";
  const cond = false;

  function process(key,value) {
        if(key === condition[condition.length-1]) //condition[condition.length-1] return currency
        {
        cond = true;
        console.log(key + " : "+value);
        }
    }
    
    function traverse(o,func) {
        for (var i in o) {
            func.apply(this,[i,o[i]]);  
            if (o[i] !== null && typeof(o[i])=="object") {
                if(cond === false)
                {
                traverse(o[i],func);
                }
            }
        }
    }
    
    traverse(SampleJSON, process);

The above code print the first occurrence of the "Currency" element but I just want to print the element based on the arg1 which is given by user, and want to replace that value based on arg2.

is there any npm package available to achieve this?

Any help would be highly appreciated.

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84
  • The title doesn't make sense. A specific property (given by the user input) will always be unique. So why the _"multiple time duplicated..."_ part? – Andreas Aug 24 '21 at 08:39
  • _"Is there any npm package is available to achieve this?"_ - Asking for libraries or other off-site resources is off-topic – Andreas Aug 24 '21 at 08:40
  • Actually User want to change the currency element which may occur multiple times in the SampleJSON file. but User interested to change the currency which is under transactions and not under items[] – ArrchanaMohan Aug 24 '21 at 08:41
  • Hence the "path" is `product.transactions.currency` – Andreas Aug 24 '21 at 08:43
  • `product.transactions` is an array (and therefor `product.transactions.currency` doesn't exist). Should this change the `currency` of every object in that array? If not, how do you specify the exact element in the array? – Andreas Aug 24 '21 at 08:43
  • Actually, the currency will not be duplicated inside the transaction array. But there may be possibilities that It will be under some other array. While I'm trying to traverse through the element tree not sure that I'm trying to get the right one instead of checking if(key === condition[condition.length-1]) this logical operations. – ArrchanaMohan Aug 24 '21 at 08:46

1 Answers1

0

You can use Lodash set method to assign a value on a specific path in an object. First, run the below command to install the package:

yarn install lodash.set

Usage

const set = require('lodash.set');

set(yourObject, 'product.transactions.currency', 'EUR');

ziishaned
  • 4,944
  • 3
  • 25
  • 32
  • 1
    Please search for duplicates (and close-vote as such) first. – Andreas Aug 24 '21 at 08:47
  • I already went through the duplicate which is not solving my problem, and andreas thanks for sharing the links. And zeeshan Ahamad thank for sharing what I'm exactly looking for. You saved my time. Once again thanks much. – ArrchanaMohan Aug 24 '21 at 08:52
  • @ArrchanaMohan literally all the duplicates suggested solve your issue – Sorin Sep 12 '21 at 21:06