0

I need to format the JSON array so that it looks like the example below:

{
"contacts":{
"1":{
"contact_email":"michasssel1@email.com",

},
"2":{
"contact_email":"michsssaael2@email.com",  
}
}
}

The current JSON looks like this for the code below. This Json array comes from the FinalEvClass class.

   [
    {
        "contacts": {
            "email": "gio@look.com",
            "name": "gio",
 
         
        },
            "contacts": {
            "email": "gioss@look.com",
             "name": "gioss",
         
        },
        
    }
    ]

Current Code for function app :

public static class ContactsBulk
{
    [FunctionName("ContactsBulk")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        //dynamic data = JsonConvert.DeserializeObject(requestBody);

        #region DeserializeObjects

        PayloadReceivedContactsBulkArray desrializepayloadReceivedContactsBulkArray = JsonConvert.DeserializeObject<PayloadReceivedContactsBulkArray>(requestBody);

        List<EvContactsBulk> evContactsBulkList = new List<EvContactsBulk>();
        List<PayloadReceivedContactsBulkArray> payloadReceivedContactsBulkArrays = new List<PayloadReceivedContactsBulkArray>();
        List<FinalEvClass> finalEvClasses = new List<FinalEvClass>();

        foreach (var item in desrializepayloadReceivedContactsBulkArray.contactsBulk)
        {
            EvContactsBulk evContactsBulk = new EvContactsBulk
            {
                email = item.email,
                name = item.name,
            };

            FinalEvClass finalEvClass = new FinalEvClass
            {
                contacts = evContactsBulk
            };

            finalEvClasses.Add(finalEvClass);
            evContactsBulkList.Add(evContactsBulk);
        };

        #endregion

        return new OkObjectResult(finalEvClasses);
    }
}

Classes for function app:

#region Contacts Class
public class PayloadReceivedContactsBulkArray
{
    public List<PayloadReceivedContactsBulk> contactsBulk { get; set; }
}

public class PayloadReceivedContactsBulk
{
    public string email { get; set; }
    public string name { get; set; }
}

public class EvContactsBulk
{
    public string email { get; set; }
    public string name { get; set; }
}

public class EvContactBulkArray
{
    public List<EvContactsBulk> contacts { get; set; }
}

public class FinalEvClass
{
    public EvContactsBulk contacts { get; set; }
}
#endregion

What to I need to do to make the JSON array look like the above example?

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
Giovan
  • 5
  • 3
  • Does this answer your question? [How do I get formatted JSON in .NET using C#?](https://stackoverflow.com/questions/2661063/how-do-i-get-formatted-json-in-net-using-c) – mousetail Sep 21 '20 at 19:33
  • 2
    So you want your JSON to be impossible to read but still have linebreaks? – Aluan Haddad Sep 21 '20 at 19:33
  • Represent both formats of data, transform the data to the desired class, serialize the data. – crashmstr Sep 21 '20 at 19:35
  • there is no _technical_ reason requiring that format. but have you tried something so far? my first attempt would be regex-replacing any amount of whitespaces following a linebreak (and hoping the data does _not_ contain any unescaped linebreaks) – Franz Gleichmann Sep 21 '20 at 20:26
  • Everyone's focusing on the (dreadful) presentation, but it seems to me your real issue is the structure - you've got an array, whereas you seem to want an object with numbered properties instead. Is there a specific reason for that? An array is really a more logical representation of a list like this - and it has (implicitly) numbered indices already. So it's hard to see what you'd gain by changing it. At best, you'll just make it harder for some clients to deserialise the data. Your desired format also seems to omit the "name" property - is that deliberate? – ADyson Sep 21 '20 at 23:22
  • This is not the code that I am exposing to the the customer. This is what I have to send to the API endpoint, and yes it is dreadful, that I is way I asking for help. – Giovan Sep 22 '20 at 06:04

0 Answers0