-1

I am using Newtonsoft's Json.Net. I have this Json:

var j1 = {"status_code":"200", "message":"everything is ok"};

and this is the other Json:

var j2 = {"records":[{"id":"1", "name":"file1"},
                      {"id":"2", "name":"file2"},
                      {"id":"3", "name":"file3"}]
         }; 

Basically the second j2 is list (array) of files in a directory. It might be empty:

 {"records":"[]"}

What I want: is to append (concat, merge) j2 to end of j1 like this:

var j3 = {
          "status_code":"200", 
          "message":"everything is ok", 
          "records":[{"id":"1", "name":"file1"},
                      {"id":"2", "name":"file2"},
                      {"id":"3", "name":"file3"}]
         };
         // NOTE: the array does NOT have double quots ("")

How can I accomplish this with JsonConvert.Serialize() or any other ways in Newtonesoft's Json.NET?

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
  • 1
    Can you please tell us what code you have written to achieve this? – JunaidKirkire Apr 21 '15 at 15:00
  • 2
    It would help if you'd provide real code rather than pseudo-code... we don't know what you're using in terms of LINQ to JSON etc. – Jon Skeet Apr 21 '15 at 15:01
  • It's not duplicate. This is appending feilds to the Json object. Not merging. I haven't coded this – A-Sharabiani Apr 21 '15 at 15:06
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Apr 21 '15 at 15:22

5 Answers5

0
var j = JsonConvert.SerializeObject(new[] { JsonConvert.DeserializeObject(j1), 
JsonConvert.DeserializeObject(j2) });

or

var j = JsonConvert.SerializeObject(new { obj1 = JObject.Parse(j1), 
                                          obj2 = JObject.Parse(j2) });
pregoli
  • 100
  • 7
0

Might try this (note: no null checking whatsoever...):

string json1 = "{\"status_code\":\"200\",\"message\":\"everything is ok\"}";
string json2 = "{\"records\":[{\"id\":\"1\",\"name\":\"file1\"},{\"id\":\"2\",\"name\":\"file2\"},{\"id\":\"3\",\"name\":\"file3\"}]}";

dynamic object1 = JsonConvert.DeserializeObject(json1);
dynamic object2 = JsonConvert.DeserializeObject(json2);

dynamic result = object1;
result.records = object2.records;

string serialized = JsonConvert.SerializeObject(result);
stefankmitph
  • 3,236
  • 2
  • 18
  • 22
0

This might work.

var j3 = j1.concat(j2);
Celeo
  • 5,583
  • 8
  • 39
  • 41
siddhipur
  • 121
  • 1
  • 13
0

These answers are vastly over complicating things... Just create a new anonymous type inline where you call SerializeObject, it would look something like this;

  string serialized = JsonConvert.SerializeObject(new { status_code = j1.status_code, 
                                                        message = j1.message,
                                                        records = j2.records } );                                            
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
0

You can achieve this by using JSON.NET built in LINQ to JSON:

string j1 = {"status_code":"200", "message":"everything is ok"};
string j2 = {"records":[{"id":"1", "name":"file1"},
                          {"id":"2", "name":"file2"},
                          {"id":"3", "name":"file3"}]
             }; 

JObject jObject1 = JObject.Parse(j1);
JObject jObject2 = JObject.Parse(j2);

jObject1.Merge(jObject2, new JsonMergeSettings
                {
                    MergeArrayHandling = MergeArrayHandling.Concat
                });

string j3 = jObject1.ToString();

The j3 variable will look exactly like this:

{
    "status_code": "200",
    "message": "everything is ok",
    "records": [
        {
            "id": "1",
            "name": "file1"
        },
        {
            "id": "2",
            "name": "file2"
        },
        {
            "id": "3",
            "name": "file3"
        }
    ]
}

which is what you wanted.

Ilija Dimov
  • 5,221
  • 7
  • 35
  • 42