Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to append ajson tag to existing json string in asp.net with c#. My current json string is as below:
{
  "RemittanceData": [
    {
      "Information": "TEST501"
    }
  ],
  "PaymentOrderProductId": "AHBDDA501",
  "DebitAccountId": "018061058118",
  "DebitCurrency": "AED",
  "DebitValueDate": "20221109",
  "CreditAccountId": "028676488149",
  "PaymentCurrencyId": "AED",
  "Amount": 10000,
  "EndToEndReference": "049500130811290W20221109132539460"
}

Desired json string:
{
"Data": {
"RemittanceData": [
{
"Information": ""
}
],
"PaymentOrderProductId": "AHBDDA501",
"DebitAccountId": "30003198541"
"DebitCurrency": "AED",
"DebitValueDate": "20220307",
"CreditAccountId": "30000008547"
"PaymentCurrencyId": "AED",
"Amount": 1,
"EndToEndReference": "TESTDDA501"
}
}


What I have tried:

Desired json string:
{
"Data": {
"RemittanceData": [
{
"Information": ""
}
],
"PaymentOrderProductId": "AHBDDA501",
"DebitAccountId": "30003198541"
"DebitCurrency": "AED",
"DebitValueDate": "20220307",
"CreditAccountId": "30000008547"
"PaymentCurrencyId": "AED",
"Amount": 1,
"EndToEndReference": "TESTDDA501"
}
}
Posted
Updated 14-Nov-22 5:37am
Comments
PIEBALDconsult 14-Nov-22 19:01pm    
What's a "json tag"?

1 solution

JSON strings aren't anything special: they are literally just a string that contains text that can be interpreted as JSON data. Or manipulated in any way that a string can.

So if you want to add text to the beginning and end, you would do it in exactly the same way you do with any other string:
C#
string result = "{\n\"Data\": " + myStringWithJsonInIt + "}\n";
But yours probably need better manipulation that that, and the best way to do that would be to read your JSON into C# classes, construct new classes that contain the manipulated data, and write a new JSON string from those.
Otherwise, you are going to be faffing with strings a lot, and probably getting a lot of bits wrong - especially as it's not too obvious where some of the data in your desired string is coming from ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900