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?

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

How would i serialize the two objects together?
Solved it Thanks a lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.