Parsing Json data from Api GET
jquery - How to get and display JSON API data with JavaScript - Stack Overflow
javascript - How to fetch an array object from API json file - Stack Overflow
How to get Json Data from an external WEB API to an JQUery autosearch text box end point
Videos
people is an array of three objects. You'll need to loop through each to grab the names. You can use array.map() to convert an array of people to an array of names:
const names = data.people.map(p => p.name) // Neil..., Buzz..., Sally...
You need to extract only the name of each person :
JSON.stringify( data.people.map( person => person.name) )
Hi @Jose Daniel Navarro Brito ,
Looking at your jQuery autocomplete issue, I can see the problem clearly. You're getting double-encoded JSON because you're treating JSON as a string when it should be handled as an object.
Here's what's happening and how to fix it:
Your API returns proper JSON, but you're converting it to a string with ReadAsStringAsync(), then trying to return that string as JSON again. This creates the double-encoding you're seeing.
You need to deserialize the JSON string back into an object, then return that object as JSON:
[HttpGet]
public async Task AutoComplete(string search)
{
var httpClient = _httpClientFactory.CreateClient("HousingWebAPI");
using (var response = await httpClient.GetAsync("AsyncAutocompleteErf/" + search))
{
if (response.IsSuccessStatusCode)
{
var jsonString = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject>(jsonString);
return Json(data);
}
}
return Json(new List()); // Return empty array instead of null
}
Why this works:
- Your external API returns valid JSON
- You deserialize it into C# objects
- You return those objects as JSON using
Json() - jQuery receives proper JSON objects, not encoded strings
Regarding GET vs POST:
GET is perfectly fine for autocomplete. You're just retrieving data based on a search term, which is exactly what GET is designed for. POST would be overkill here.
Hope this helps!
it you look at the response in the browser debug tools the json string is probably encoded, because the AutoComplete action returned a string that is converted to a json. for example:
the string value [{"value":1}] when converted to json is: "[{\"value\":1}]"
the method Json is expecting an object to convert, not a string that contain Json Data. you ned to return content of type "application/json".
var content = await response.Content.ReadAsStringAsync();
return Context(content,"application/json");
So, he's telling me that frontend frameworks like React aren't performant if you use JSON arrays. He wants the JSON arranged to use only objects such as:
{ "key1": { "subkey1": "value1" }, "key2": { "subkey1": "value1" }, ... }
Rather than: [ { "id": "key1", subkey1": "value1" }, { "id": "key1", subkey1": "value1" }, ... ]
It's not really a big deal, but I just don't understand how using JSON arrays isn't performant. It seems weird, what am I missing?
Edit: Thanks for the Gold dude! It looks like my teammate gave me a Reddit gold. To all of you devs that participated in the discussion, thank you very much! We found some very clear answers that we can take with us going forward.