2

I have the ff. JSON payload of an Array

  {
    "Meta": {
        "LastAvailableDateTime": "",
        "FirstAvailableDateTime": "",
        "TotalPages": 1,
        "Count": 2
    },
    "Links": {
        "Next": null,
        "Previous": null,
        "Self": ""
    },
    "Data": {
        "Transactions": [
            {
                "AuthCode": "175601",
                "CardId": "************8104"
            },
            {
                "AuthCode": "783453",
                "CardId": "************8104"
            },
           {

                "AuthCode": "12132",
                "CardId": "************8104"
            }
        ]
    }
}

And I want to override this payload by renaming CardId to MaskedCardId and adding a new param named CardId with an actual Card id.

My code is the ff. below

var json = //The Parsed JSON Payload;
var CardId = "123458104"

json = JSON.parse(
       JSON.stringify(json).split('"CardId":')
           .join('"CardId":CardId,"MaskedCardId":')
);

I could already replace the CardId with the renamed parameter named MaskedCardId. with the ff. code

 json = JSON.parse(JSON.stringify(json).split('"CardId":').join('"MaskedCardId":'));

But whenever I try to add a new parameter named CardId with a variable value, it is giving an error of.

failed with error: Javascript runtime error: "SyntaxError: Unexpected token: C

How do I make this line of code work with my intended purpose?. TIA

iamjoshua
  • 1,157
  • 3
  • 16
  • 33
  • 2
    Why not `json.MaskedCardId = json.CardId` and `json.CardId = CardId`? – lusc Aug 04 '21 at 15:06
  • cuz I am working on a JSON array. I already updated the sample payload as it seems to resemble the closest payload I am working on. – iamjoshua Aug 04 '21 at 15:18

3 Answers3

5

Remember the Old value

const data = {
  "AuthCode": "1",
  "PostingDate": "2021-05-06",
  "Amount": "6000.28",
  "CardId": "************8104",
};

const CardIdNew = "123458104";
const CardIdOld = data.CardId;

data.CardId       = CardIdNew;
data.MaskedCardId = CardIdOld;

console.log(data);

Or to put it simpler:

const data = {
  "AuthCode": "1",
  "PostingDate": "2021-05-06",
  "Amount": "6000.28",
  "CardId": "************8104",
};

data.MaskedCardId = data.CardId;   // Add 
data.CardId       = "123458104";   // Override 

console.log(data);

ADDENDUM after the Question edit

Loop the Array containing the Transactions:

const json = {"Meta":{"LastAvailableDateTime":"","FirstAvailableDateTime":"","TotalPages":1,"Count":2},"Links":{"Next":null,"Previous":null,"Self":""},"Data":{"Transactions":[{"AuthCode":"175601","CardId":"************8104"},{"AuthCode":"783453","CardId":"************8104"},{"AuthCode":"12132","CardId":"************8104"}]}};

const CardId = "123458104"

json.Data.Transactions.forEach(ob => {
  ob.MaskedCardId = ob.CardId;
  ob.CardId = CardId;
});

console.log(json);
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • Hi, I updated my JSON payload in the question to resemble what Im really working on, Im trying to modify the parameters and values inside a JSON array, can you check?. TIA – iamjoshua Aug 04 '21 at 15:14
  • 1
    noted, thx. Sorry. Had to modify it since the original payload is long and I seem to have over redacted the sample payload. – iamjoshua Aug 04 '21 at 15:17
  • 1
    @iamjoshua expanded my answer with another example. – Roko C. Buljan Aug 04 '21 at 15:25
1

I guess this should work. The join must be like this .join('"CardId":"' + CardId + '", "MaskedCardId":') since you are adding a string variable into the object, don´t forget to put the "' + CardId + '":

var json = //The Parsed JSON Payload;
var CardId = "123458104"

json = JSON.parse(
       JSON.stringify(json).split('"CardId":')
           .join('"CardId":"' + CardId + '", "MaskedCardId":')
);

Try it here

var json = {
  someId: 10,
  somekey: "somevalue",
  cards: [
     {
      CardId: "*********67",
      otherkey: "othervalue"
     },
     {
      CardId: "*********67",
      otherkey: "othervalue"
     }
  ]
}
var CardId = "123458104"

json = JSON.parse(
       JSON.stringify(json).split('"CardId":')
           .join('"CardId":"' + CardId + '", "MaskedCardId":')
);

console.log(json);
AlexSp3
  • 2,201
  • 2
  • 7
  • 24
  • This worked for me, though the other answer that got the most up votes atm seem to be able to do the job, but I marked this one as the answer since Apigee doesnt support forEach on that answer. – iamjoshua Aug 04 '21 at 15:35
0

Split and join are for strings; manipulate the JSON as an object instead.

json.MaskedCardId = json.CardId;
json.CardId = CardId;
bburhans
  • 539
  • 3
  • 12