Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two json strings as shown below
JavaScript
{
  "account-Ind": "A",
  "multipage-Ind": " ",
  "relat-Ch-St-Nmbr": "0000001",
  "relat-Claim-Nmbr": "0000000",
  "schg-Remit-Flag": " ",
  "schg-Mgcare": "000000000",
  "schg-Co-Pay": "000000000",
  "schg-Deductible": "000000000",
  "schg-Co-Ins": "000000000",
  "schg-Payment": "000000000",
  "schg-Tot-Claim": "000000000",
  "schg-Elig": " ",
  "filler1": "                                                  "
}


and this is second json string
JavaScript
{
  "message-Code-1": "    ",
  "msg-Number-Pos1-1": " ",
  "msg-Number-Pos2-1": " ",
  "msg-Number-Pos3-1": " ",
  "message-Code-2": "    ",
  "msg-Number-Pos1-2": " ",
  "msg-Number-Pos2-2": " ",
  "msg-Number-Pos3-2": " ",
  "message-Code-3": "    ",
  "msg-Number-Pos1-3": " ",
  "msg-Number-Pos2-3": " ",
  "msg-Number-Pos3-3": " ",
  "message-Code-4": "    ",
  "msg-Number-Pos1-4": " ",
  "msg-Number-Pos2-4": " ",
  "msg-Number-Pos3-4": " "
}

I need an output like below
JavaScript
{
  "account-Ind": "A",
  "multipage-Ind": " ",
  "relat-Ch-St-Nmbr": "0000001",
  "relat-Claim-Nmbr": "0000000",
  "schg-Remit-Flag": " ",
  "schg-Mgcare": "000000000",
  "schg-Co-Pay": "000000000",
  "schg-Deductible": "000000000",
  "schg-Co-Ins": "000000000",
  "schg-Payment": "000000000",
  "schg-Tot-Claim": "000000000",
  "schg-Elig": " ",
  "filler1": "                                                  ",
  "message-Table":
    {
      "message-Code-1": "0000",
      "msg-Number-Pos1": "1",
      "msg-Number-Pos2": "1",
      "msg-Number-Pos3": "1",    
      "message-Code-2": "0001",
      "msg-Number-Pos1": "2",
      "msg-Number-Pos2": "1",
      "msg-Number-Pos3": "1",   
      "message-Code-3": "0002",
      "msg-Number-Pos1": "2",
      "msg-Number-Pos2": "1",
      "msg-Number-Pos3": "1",   
      "message-Code-4": "0004",
      "msg-Number-Pos1": "2",
      "msg-Number-Pos2": "1",
      "msg-Number-Pos3": "1", 
      "message-Code-5": "0005",
      "msg-Number-Pos1": "2",
      "msg-Number-Pos2": "1",
      "msg-Number-Pos3": "1"
    }
 
}


What I have tried:

JavaScript
var javaScriptSerializer = new JavaScriptSerializer();
                var userDetails = javaScriptSerializer.DeserializeObject(json1);
                var messageTable = javaScriptSerializer.DeserializeObject(json2);
                var arrayOfObjects = JsonConvert.SerializeObject(
                      new[] { JsonConvert.DeserializeObject(json1), JsonConvert.DeserializeObject(json2) });
                return arrayOfObjects;

But I getting result like below 

[
  {
    "account-Ind": "A",
    "multipage-Ind": " ",
    "relat-Ch-St-Nmbr": "0000001",
    "relat-Claim-Nmbr": "0000000",
    "record-Type": "1",
    "record-Seq": "0000",
    "cert-Nmbr": "109085006",
    "provider-Charge": "000035000",
    "int-Amount-Paid": "0000000",
    "clm-Account-Nmbr": "HNY1002     ",
    "alt-Id": "931929156",
    "schg-Elig": " ",
    "filler1": "                                                  "
  },
  {
    "message-Code-1": "    ",
    "msg-Number-Pos1-1": " ",
    "msg-Number-Pos2-1": " ",
    "msg-Number-Pos3-1": " ",
    "message-Code-2": "    ",
    "msg-Number-Pos1-2": " ",
    "msg-Number-Pos2-2": " ",
    "msg-Number-Pos3-2": " ",
    "message-Code-3": "    ",
    "msg-Number-Pos1-3": " ",
    "msg-Number-Pos2-3": " ",
    "msg-Number-Pos3-3": " ",
    "message-Code-4": "    ",
    "msg-Number-Pos1-4": " ",
    "msg-Number-Pos2-4": " ",
    "msg-Number-Pos3-4": " ",
    "message-Code-5": "    ",
    "msg-Number-Pos1-5": " ",
    "msg-Number-Pos2-5": " ",
    "msg-Number-Pos3-5": " "
  }
]
Posted
Updated 28-Jun-17 23:27pm
v2

There are a few ways to achieve this.

The first, is as you have done and return an array of objects. You would obviously need to know what object what what in the array.
You have implicitly created an array of the two objects in your example so this is what you get.

The second and third would mean that you need to de-serialize the json object(s) and add the second to the third in one of two ways:

You can either add the sting, giving you a Json object to be deserialized with a string parameter that also needs to be deserialized. This method has it's uses, but I don't think that's what you want. Either way, the process is similar to the last option:

Deserialize object 1 into a class (that you define). One of the class fields is an object (or string, see above) to deserialize object 2 into. I.E.:


C#
class Object1 {
    [JsonProperty("account-Ind")]
    public string AccountInd {get;set;}
    [JsonProperty("multipage-Ind")]
    public string MultipageInd {get;set;}
    [JsonProperty("relat-Ch-St-Nmbr")]
    public string RelatChStNmbr {get;set;}
    [JsonProperty("relat-Claim-Nmbr")]
    public string RelatClaimNmbr {get;set;}
    [JsonProperty("record-Type")]
    public string RecordType {get;set;}
    [JsonProperty("record-Seq")]
    public string RecordSeq {get;set;}
    [JsonProperty("cert-Nmbr")]
    public string CertNmbr {get;set;}
    [JsonProperty("provider-Charge")]
    public string ProviderCharge {get;set;}
    [JsonProperty("int-Amount-Paid")]
    public string IntAmountPaid {get;set;}
    [JsonProperty("clm-Account-Nmbr")]
    public string ClmAccount-Nmbr {get;set;}
    [JsonProperty("alt-Id")]
    public string AltId {get;set;}
    [JsonProperty("schg-Elig")]
    public string SchgElig {get;set;}
    [JsonProperty("filler1")]
    public string Filler1 {get;set;}
    [JsonProperty("message-Table")]
    public Object2[] MessageTable {get;set;}
}


var javaScriptSerializer = new JavaScriptSerializer();
                var userDetails = javaScriptSerializer.DeserializeObject<Object1>(json1);
                var messageTable = javaScriptSerializer.DeserializeObject<Object2>(json2);
                userDetails.MessageTable = messageTable;
                return userDetails;  //or JavaScriptSerializer.Serialize(userDetails);



I didn't specify Object2, you get the idea.

I specified that MessageTable should be an array. I think you'll have issues if you have multiple fields with the same name in the same object

I hope that's enough to get you on your way

Andy ^_^
 
Share this answer
 
Comments
Member 13284316 29-Jun-17 4:44am    
In my case I cannot prepare the class separately. Is there any way by which I can get the required result without preparing the class?
Andy Lanng 29-Jun-17 5:25am    
Well I guess you could try some direct string manipulation : Solution 2
Given that you can't deserialize into a class (for some unknown reason), try this hack:

C#
var result = json1.Insert(json1.indexOf("}"),json2)
 
Share this answer
 
Comments
Member 13284316 29-Jun-17 9:28am    
Hi Andy, your above sample did combine the json but was unable to view in the json viewer since there was error in the output file. If you observe the output that I require in my question, you can find "message-Table": before the "{".
Can you please help me in adding that? Thanks.
Andy Lanng 29-Jun-17 11:57am    
then var result = json1.Insert(json1.indexOf("}"),", message-Table: " + json2)

You may need to play around with the string, but it's just string manipulation. Look at the result and adjust accordingly. meet me half way!
Member 13284316 30-Jun-17 7:39am    
HI Andy, even I did the same. Thanks.

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