0

I have written a azure function which will return data into json string format but i want data into json object so that i can directly use that array output for next step into logic app.

azure function code -

        composeMessage = "{\"__metadata\": {\"id\": "+obj.id+",\"uri\": "+obj.uri+",\"dateForSystem\": "+obj.dateForSystem + ",\"timeForSystem\": "+obj.timeForSystem + "}";
        composeMessageList.Add(composeMessage);
        outputDerivedTableKey = string.Empty;
        startIndex = 0;
    }
     var jsonToReturn = JsonConvert.SerializeObject(composeMessageList);
    return new HttpResponseMessage(HttpStatusCode.OK) {
Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")

getting output like -

[
  "{\"__metadata\": {\"id\": ,\"uri\": ,\"type\": },\"dateForSystem\": 2019-05-17,\"timeForSystem\": 13:15:51}",
  "{\"__metadata\": {\"id\": ,\"uri\": ,\"type\": },\"dateForSystem\": 2019-05-17,\"timeForSystem\": 13:15:51}",
  "{\"__metadata\": {\"id\": ,\"uri\": ,\"type\": },\"dateForSystem\": 2019-05-17,\"timeForSystem\": 13:15:51}",
]

But I can't pass this array to foreach in logic app i'm excepting output format like below from azure function -

[
  {
    "__metadata": {
      "id": "",
      "uri": "",
      "type": ""
    },
    "dateForSystem": "2019-05-17",
    "timeForSystem": "13:15:51"
  },
   {
    "__metadata": {
      "id": "",
      "uri": "",
      "type": ""
    },
    "dateForSystem": "2019-05-17",
    "timeForSystem": "13:15:51"
  },
   {
    "__metadata": {
      "id": "",
      "uri": "",
      "type": ""
    },
    "dateForSystem": "2019-05-17",
    "timeForSystem": "13:15:51"
  },
]

How can i achieve this format output from azure function ?

Or how to format this into logic app?

1 Answer 1

2

The problem is that the serialized object is a list of string so Json.Net serializes it as an array of string.

Here is a simple function that use dynamic objects but you can also create a class for your composeMessage object:

[FunctionName("Function1")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req,
    ILogger log)
{
    var composeMessageList = new List<object>();
    for(var i = 0; i < 5; i++)
    {
        var composeMessage = new
        {
            __metadata = new
            {
                id = "",
                uri = "",
                type = ""
            },
            dateForSystem = "2019-05-17",
            timeForSystem = "13:15:51"
        };

        composeMessageList.Add(composeMessage);
    }

    var jsonToReturn = JsonConvert.SerializeObject(composeMessageList);
    return new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")
    };
}
Sign up to request clarification or add additional context in comments.

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.