0

I have a minimal API in .NET 6. In this API, I call a 3rd party API which returns normal json formatted data by using this code:

HttpResponseMessage response = await client.GetAsync(url);

if (response.IsSuccessStatusCode)
{
    jsonResponse = await response.Contet.ReadAsStringAsync
}

jsonResponse looks like this for example:

{
    "response":
    {
        "AnArray": [
                      "Id": 11,
                      "AnotherId":22
                   ],
        "AndACityArray": [
                             "New York",
                             "London",
                             "Berlin"
                         ],
       "ADirectionArray": [
                             "North",
                             "East",
                             "South",
                             "West"
                          ]
    }
}

If I return this as it is, in postman it is escaped like

{\"response\": and so on}

If I replace the escape chars it looks good, but its still plain text.

I tried to return it as it is. I tried JsonConvert.DeserializeObject to dynamic or to object. In this case, it is returned as JSON, but there are just empty arrays.

{response:[
  [
    []
  ],
  [
    []
  ],
  [
    []
  ],
}

The structure of that JSON can change, so I can't just define a class to deserialize it.

I tried different results. Results.Text, Results.JSON, converted it back and forwards.

I just want to pass the json data I get from the 3rd party API, straight through my minimal API and return it.

6
  • Json is a string if it is not a javascript object. What is the problem? Can you post the code you have tried and what is not workong properly? Commented Mar 21, 2023 at 19:50
  • @Serge the problem is, that the data is returned as string and not as JSON Commented Mar 21, 2023 at 19:52
  • It is supposed to return a string. This is why it is used over the network. Commented Mar 21, 2023 at 19:53
  • I want to return JSON, if I return a string the user of the api have to JSON.Parse the data they get in JavaScript Commented Mar 21, 2023 at 19:59
  • 2
    JSON IS strings. That's all it is: a particular way of arranging data in a string so it will be consistent and understandable on the other end. Commented Mar 21, 2023 at 20:05

1 Answer 1

4

You are trying to return a string which will be encoded as json (hence the resulting double encoded json string), use Results.Content:

return Results.Content(jsonResponse); 

JsonConvert.DeserializeObject does not work because Minimal APIs use System.Text.Json, not Newtonsoft.Json (where JsonConvert comes from), so the underlying dynamic object created by it seems not being processed correctly. If you want to stick with serialize-deserialize approach you should look into corresponding System.Text.Json APIs (like JsonNode or JsonDocument, also note that sample JSON is not a valid one, so if it is actually your JSON and not a typo, then you will need to fix it first). But I would argue that the content approach is better choice here.

Sign up to request clarification or add additional context in comments.

14 Comments

You Sir, have saved my night. Results.Content(jsonResponse, contentType: "application/json") solved my problem. Thank you!
@John you just doing a double job. It was converted to json string, now you have deserialized it to object, and after this the object was converted to a json string again by API action response.
@Serge there is no double job in Results.Content approach.
@GuruStron I am sorry, but it is your opinion, that is my opinion. I don't belive that iResults.Content(jsonResponse, contentType: "application/json") sends a binary object
@GuruStron Ok, it is just misunderstanding. I think I understand what OP mean but the question is not correct. it will return a json string in any case, but I think if json returns as a string it will be serialized twice and will need double deserialization too. I think JObject.Parse will do the same but am sure there is a better way. But I am sorry, it is 2am here already and I had a bad day and very tired. I will give my answer tommorrow.
|

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.