Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat two json strings in c#

Tags:

json

c#

Let's say I have two JSON strings to start: (example from json.net)

string json1 = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

and 

string json2 = @"{
  'Name': 'Good Ol Boys',
  'ReleaseDate': '2995-4-7T00:00:00',
  'Genres': [
    'Action',
    'None'
  ]
}";

What is the way to concatenate these two json strings into one json string? I've looked at a few different items here, and they don't fit the bill, primarily because they MERGE the two objects into one - I haven't seen a whole lot that just gives me two objects in one string. I found this answer, but it's for javascript, not C#. It is, however, close to what I want.

The answer marked as correct for a similar question contains no explanation, nor enough code to provide the proper context for me to make sense out of it. And it's closer to what I'm looking to do, because it has the "data" element in it...whereas the answer with the greater number of votes merges and in some way overwrites one of my objects, leaving me with one, instead of two.

I'm not ready to deserialize the string just yet, as I don't know how many objects I will get in advance. In other words, I want to put all the json object strings into one big string, then I can deserialize the whole string all at once.

How do I do that?

EDIT

Turns out that I was asking how to concat two arrays, which is not the same thing at all. So my question was wrong. This answer is an answer to the question I referenced above. What I ended up doing is deserializing each array, then doing the AddRange(d2.data) method, and reserializing so that I could then deserialize the whole thing into one big JSON object. Which I then use elsewhere.

And another thing - C# thinks of strings as strings, but even though JSON objects are technically strings, they're really not, which was part of my confusion. I also did not understand how JSON objects work, in terms of what is an array and what is a string...I had seen some answer on SO that basically said to make sure you understand what objects are in JSON, etc. Anyway...problem solved.

like image 971
NovaDev Avatar asked Dec 14 '22 23:12

NovaDev


2 Answers

This might be a bit overkill, but you'll be guaranteed to get a meaningful error in the case of malformed data, or you'll get valid JSON.

var arrayOfObjects = JsonConvert.SerializeObject(
    new[] { JsonConvert.DeserializeObject(json1), JsonConvert.DeserializeObject(json2) }
)

We deserialize each json object into object (so we don't need to care about the structure), wrap it in an object[], and serialize it back into JSON. Unless you're parsing a huge amount of objects, this should be sufficiently performant.

like image 195
Rob Avatar answered Dec 17 '22 01:12

Rob


In order to combine those 2 Json strings, you need to have a bigger entity that allow you to hold any json you want, in this case just put and "[" at the start of the string, then put a comma between json strings "," and finally close it "]"

string json = "[" + json1 + "," + json2 + "]";

This is the result:

[
{
'Name': 'Bad Boys',
'ReleaseDate': '1995-4-7T00:00:00',
'Genres': [
'Action',
'Comedy'
]
},
{
'Name': 'Good Ol Boys',
'ReleaseDate': '2995-4-7T00:00:00',
'Genres': [
'Action',
'None'
]
}
]

Hope it helps.

like image 40
Luis Leon Gonzalez Avatar answered Dec 17 '22 01:12

Luis Leon Gonzalez