4

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.

Community
  • 1
  • 1
NovaDev
  • 2,737
  • 5
  • 29
  • 43

4 Answers4

10

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.

Rob
  • 26,989
  • 16
  • 82
  • 98
2

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.

0

what's wrong with

string combinedjson = json1 + json2;

unless I don't understand the question.

David Green
  • 1,210
  • 22
  • 38
  • I'm assuming this doesn't work because then the deserialization won't work because the objects aren't formed as an array of separate objects – Mark C. Mar 27 '17 at 01:48
  • This makes no sense because the result is not JSON anymore. – Quality Catalyst Mar 27 '17 at 01:48
  • Well, that would produce invalid JSON, which breaks the requirement they have: `"then I can deserialize the whole string all at once."` – Rob Mar 27 '17 at 01:48
  • I answered the question asked, which was how to concat 2 strings. @Novadev also stated he was not ready to deserialize the JSON. He'd obviously have to turn the 1 string back into an array for deserialization, or find a different way of dealing with it. – David Green Mar 28 '17 at 03:00
0

what you can do is built a json array as a string which you can deseralize where it is needed. so you can do like

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

if you want to do loop then

string jsonArrayString ="[";

inside loop

{
jsonArrayString =jsonArrayString +","+currentJsonItem;
}
jsonArrayString =jsonArrayString +"}";
Ramesh Kharbuja
  • 407
  • 2
  • 8