1

I already checked the duplicate question and tried same code but doesn't work.

I have two json serialized objects and want to return those two outputs .

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Text;

public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);

    var httpResult = data.a;
    var sqlData = data.b;

    var matchedList = new List<dynamic>();

        var unmatchedList = new List<dynamic>();


            foreach (var itemHttp in httpResult)
            {
                foreach (var itemSql in sqlData)
                {
                    if (itemHttp.name == itemSql.tablename)
                    {
                        matchedList.Add(itemHttp);
                    }
                    else{

                        unmatchedList.Add(itemHttp.name);
                    }
                }
            }

    var jsonToReturn1 = JsonConvert.SerializeObject(matchedList);

    var jsonToReturn2 = JsonConvert.SerializeObject(unmatchedList);


     return new HttpResponseMessage(HttpStatusCode.OK) {
        Content = new StringContent( new { a= new{jsonToReturn1}, b= new {jsonToReturn2}}, Encoding.UTF8, "application/json")
    };


}

input used for function -

{
  "a": [
    {
      "id": "1",
      "name": "aaa"
    },
    {
      "id": "2",
      "name": "bbb"
    },
    {
      "id": "3",
      "name": "ccc"
    },
    {
      "id": "4",
      "name": "ddd"
    }
  ],
  "b": [
    {
      "id": "111",
      "tablename": "aaa"
    },
    {
      "id": "222",
      "tablename": "bbb"
    }
  ]
}
1
  • 1
    Please define "doesn't work". My intuition for your suggested solution implies that you will return a json document with two properties a and b, which each have one property named jsonToReturn1|2, whose value is a string containing the json you actually want. This doesn't sound like a correct outcome, but it "works" in that it returns JSON. Is that what happens; does something else happen; if you receive an error response, what is the error? Please specify exactly what you expected and what you saw instead. Commented Jul 20, 2019 at 19:24

2 Answers 2

4

There are two ways:

The first one (more preferable)

public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{
    dynamic data = await req.Content.ReadAsAsync<object>();

    // ..

    var result = new
    {
        a = matchedList,
        b = unmatchedList
    };

    /* The mediaType-param with value 'JsonMediaTypeFormatter.DefaultMediaType' can be omitted. */
    return req.CreateResponse(HttpStatusCode.OK, result, JsonMediaTypeFormatter.DefaultMediaType);
  } 

Second one

public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{
    dynamic data = await req.Content.ReadAsAsync<object>();

    // ..

    var payload = JsonConvert.SerializeObject(new
    {
        a = matchedList,
        b = unmatchedList
    });

    var content = new StringContent(payload, Encoding.UTF8, "application/json");

    return new HttpResponseMessage(HttpStatusCode.OK) { Content = content };
} 
Sign up to request clarification or add additional context in comments.

Comments

0

Add them to a container which can replicate the structure you want (a dictionary will do), then return the serialized container:

var container = new Dictionary<string, object>()
{
    { "a", matchedList},
    { "b", unmatchedList}
};
string json = JsonConvert.SerializeObject(container);

Comments

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.