0

I am using .net for api and using system.text.json..

I have two differnt json strings as per below and I want to create one json string from that two strings, like I want to merge both strings in one but it should look like below. I am giving both json strings below and also the one which I am expecting also.. if any can help me..

First json string which I have:

{
  "FirstJson": {
    "Test1": {
      "2020": [
        "40%",
        "40%",
        "40%",
        "40%"
      ],
      "2021": [
        "50%",
        "30%",
        "30%",
        "40%"
      ]
    },
    "Test2": {
      "2020": [
        "No",
        "No",
        "No",
        "No"
      ],
      "2021": [
        "No",
        "No",
        "No",
        "No"
      ]
    }
  }
}

Second string which I have:

{
  "SecondJson": {
    "Test3": [
      {
        "2020": [
          "LC",
          "Cd",
          "NA",
          "LP"
        ],
        "2021": [
          "LC",
          "Cd",
          "NA",
          "Lp"
        ]
      },
      {
        "2020": [
          "LC",
          "LC",
          "LC",
          "LC"
        ],
        "2021": [
          "Cd",
          "Cd",
          "Cd",
          "Cd"
        ]
      }
    ]
  }
}

Now I want to merge both and want to create one so below is string which I am expecting to create:

{
  "FirstJson": {
    "Test1": {
      "2020": [
        "40%",
        "40%",
        "40%",
        "40%"
      ],
      "2021": [
        "50%",
        "30%",
        "30%",
        "40%"
      ]
    },
    "Test2": {
      "2020": [
        "No",
        "No",
        "No",
        "No"
      ],
      "2021": [
        "No",
        "No",
        "No",
        "No"
      ]
    }
  },
  "SecondJson": {
    "Test3": [
      {
        "2020": [
          "LC",
          "Cd",
          "NA",
          "LP"
        ],
        "2021": [
          "LC",
          "Cd",
          "NA",
          "Lp"
        ]
      },
      {
        "2020": [
          "LC",
          "LC",
          "LC",
          "LC"
        ],
        "2021": [
          "Cd",
          "Cd",
          "Cd",
          "Cd"
        ]
      }
    ]
  }
}
Sami In
  • 246
  • 2
  • 11
  • How random are those strings/objects?.. - I would say all you need is to deserialize both into an object with both 'FirstJson' and 'SecondJson' properties and serialize it back. Alternatively if you can switch to Json.NET - there is a merge function there https://www.newtonsoft.com/json/help/html/MergeJson.htm – cycaHuH Aug 23 '23 at 13:36

0 Answers0