0

So I have 2 json arrays as string in the below variables. They both have the header "invoices" and I would like to merge the two together so there is only 1 header and 4 items inside.

currently have:

var info1 = {"invoices":[{"url":"https://api.freeagent.com/v2/invoices/1","contact":"https://api.freeagent.com/v2/contacts/1"},{"url":"https://api.freeagent.com/v2/invoices/2","contact":"https://api.freeagent.com/v2/contacts/2"}]}

var info2 = {"invoices":[{"url":"https://api.freeagent.com/v2/invoices/3","contact":"https://api.freeagent.com/v2/contacts/3"},{"url":"https://api.freeagent.com/v2/invoices/4","contact":"https://api.freeagent.com/v2/contacts/4"}]}

Desired outcome:

var info3 = {"invoices":[{"url":"https://api.freeagent.com/v2/invoices/1","contact":"https://api.freeagent.com/v2/contacts/1"},{"url":"https://api.freeagent.com/v2/invoices/2","contact":"https://api.freeagent.com/v2/contacts/2"},{"url":"https://api.freeagent.com/v2/invoices/3","contact":"https://api.freeagent.com/v2/contacts/3"},{"url":"https://api.freeagent.com/v2/invoices/4","contact":"https://api.freeagent.com/v2/contacts/4"}]}

Is there any functions that I can use to do this?

brian4342
  • 1,265
  • 8
  • 33
  • 69

1 Answers1

1

The easiest way would be to deserialize them to 2 instances of the same class, add the array items together, and then serialize the object back to string.

Info info1 = // deserialize info1
Info info2 = // deserialize info2

info1.Invoices.AddRange(info2.Invoices);

string json = // serialize info1

Types:

class Info
{
    List<Invoice> Invoices;
}

class Invoice
{
    string URL;
    string Contact;
}
i3arnon
  • 113,022
  • 33
  • 324
  • 344