C
C#3y ago
RiamuYui

Deserializing JSON from HttpClient

I seem to not know the syntax to deserialize the JSON I'm getting back from the api, I'm getting a 400 error "Value cannot be null. (Parameter 'source')". If anyone could give any insight that would be wonderful.
221 Replies
square.
square.3y ago
maybe you have a null param?
RiA
RiA3y ago
did you try var response = ....... as OSMResponseModel;
RiamuYui
RiamuYuiOP3y ago
Same error as in the json is returning a null parameter?
RiA
RiA3y ago
check if the stream the deserializer is supposed to parse isn't null
RiamuYui
RiamuYuiOP3y ago
as in the raw json?
RiA
RiA3y ago
additionally green squigle means it's might be null. you're not initializing display_name with a non null value either use string? instead perhaps
RiamuYui
RiamuYuiOP3y ago
surely I wouldn't have to initialize display_name as anything if it is getting a result through the get request still coming back with the error
RiA
RiA3y ago
can't find a 'source' param in JsonSerializer class. maybe try logging the error or have a breakpoint
square.
square.3y ago
can u share the response body? you might be trying to parse a json array instead of a json object containing an array
RiamuYui
RiamuYuiOP3y ago
RiA
RiA3y ago
might be a good idea to catch the data entirely then do a synchronous deserialize. you'll be able to see what you're deserializing
RiamuYui
RiamuYuiOP3y ago
I put one over the serialization and it goes to the error and says 404
square.
square.3y ago
i think you need to be returning a
{
[
{
}
]
}
{
[
{
}
]
}
for it to parse properly
RiA
RiA3y ago
i dont think that's the issue
RiamuYui
RiamuYuiOP3y ago
i believe that's there
RiamuYui
RiamuYuiOP3y ago
to do it synchronously how would i go about it
square.
square.3y ago
the response is of type OSMOutputs ur trying to parse it into type OSMResponseModel
RiA
RiA3y ago
no OSMOutput is just a member of OSMResponseModel
RiamuYui
RiamuYuiOP3y ago
it didn't enjoy being parsed as OSMOutputs at all
Cisien
Cisien3y ago
Your model is expecting the json to have one property thats an array at the root But the json you posted isnt that
RiamuYui
RiamuYuiOP3y ago
Agreed, I don't know how to parse it all as one though
Cisien
Cisien3y ago
What does the full raw json look like?
RiamuYui
RiamuYuiOP3y ago
{"place_id":109189975,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"way","osm_id":22724318,"lat":"52.00201252464646","lon":"0.9994463968148076","display_name":"Wenham Magna, Little Wenham, Babergh, Suffolk, England, CO7 6PS, United Kingdom","address":{"village":"Little Wenham","city":"Babergh","county":"Suffolk","ISO3166-2-lvl6":"GB-SFK","state":"England","ISO3166-2-lvl4":"GB-ENG","postcode":"CO7 6PS","country":"United Kingdom","country_code":"gb"},"boundingbox":["52.0005781","52.0079506","0.9918249","1.0162949"]}
RiA
RiA3y ago
if you need to concatenate and serialize, you can create another type and have the two types as members of it
Cisien
Cisien3y ago
What? So you only get exactly one entry?
square.
square.3y ago
no need to concatenate though, serializer would just drop properties not found in the model
RiA
RiA3y ago
class Thing3 { Thing1; Thing2; }, then serialize Thing3 instead of 1 and 2
square.
square.3y ago
iirc
RiamuYui
RiamuYuiOP3y ago
all i want to do is retrieve the variables of lat lon and display_name from the json
Cisien
Cisien3y ago
Using your OSMOutouts class as the parameter to postasjsonasync should work
square.
square.3y ago
i aspire to attain the level of verbosity that this man displays when explaining code
RiamuYui
RiamuYuiOP3y ago
Cisien
Cisien3y ago
You can try postasync, and read the content as a string to see what you get back
RiA
RiA3y ago
this is what i was trying to say earlier. just didn't know the syntax for it
Cisien
Cisien3y ago
Response.content.reasasstringasync
RiamuYui
RiamuYuiOP3y ago
RiamuYui
RiamuYuiOP3y ago
if i switch the capitalisation it does nothing either
square.
square.3y ago
response is already of type OSMOutputs
Cisien
Cisien3y ago
That was phone text
RiA
RiA3y ago
i think Cisien meant use PostAsync instead of GetFromJsonAsync
Cisien
Cisien3y ago
Client.PostAsync Then read the content Or GetAsync i guess
RiamuYui
RiamuYuiOP3y ago
Replacing GetFromJsonAsync with either of those shows the error "cannot be used with type arguments"
RiA
RiA3y ago
remove the generic <...>
Cisien
Cisien3y ago
Yeah, ditch the type arg The json methods are helper extensions that wrap manually doing the deserialization
RiamuYui
RiamuYuiOP3y ago
okay the response im getting back is not what im expecting { "version": "1.1", "content": { "headers": [ { "key": "Content-Type", "value": [ "application/json; charset=utf-8" ] } ] }, "statusCode": 200, "reasonPhrase": "OK", "headers": [ { "key": "Server", "value": [ "nginx" ] }, { "key": "Date", "value": [ "Tue, 20 Sep 2022 13:45:32 GMT" ] }, { "key": "Transfer-Encoding", "value": [ "chunked" ] }, { "key": "Connection", "value": [ "keep-alive" ] }, { "key": "Keep-Alive", "value": [ "timeout=20" ] }, { "key": "Access-Control-Allow-Origin", "value": [ "*" ] }, { "key": "Access-Control-Allow-Methods", "value": [ "OPTIONS,GET" ] } ], "trailingHeaders": [], "requestMessage": { "version": "1.1", "versionPolicy": 0, "content": null, "method": { "method": "GET" }, "requestUri": "https://nominatim.openstreetmap.org/reverse?format=json&limit=1&lat=52&lon=-1", "headers": [ { "key": "User-Agent", "value": [ "Mozilla/5.0", "(Windows NT 10.0; Win64; x64)", "AppleWebKit/537.36", "(KHTML, like Gecko)", "Chrome/105.0.0.0", "Safari/537.36", "Edg/105.0.1343.33" ] }, { "key": "traceparent", "value": [ "00-9eafc6b21c22e0e4a3382cabb1a0d5ad-02e8d08db0e28f17-00" ] } ], "properties": {}, "options": {} }, "isSuccessStatusCode": true }
Cisien
Cisien3y ago
You got back an html page in json format Yeah, so figure that one out, then your code has a chance of working 🙂
square.
square.3y ago
wait, how'd you know it was a webpage? isn't it a generic http response from an api?
RiamuYui
RiamuYuiOP3y ago
😂 why thank you, but now I have even less of an idea
RiA
RiA3y ago
look at the code, it has a useragent and html's head restful stuff are supposed to send out exactly what you need
RiamuYui
RiamuYuiOP3y ago
okay but why would it send back this and not the json from the requestUri
Cisien
Cisien3y ago
Is long supposed to be -1?
square.
square.3y ago
oh i see it now lmao
RiamuYui
RiamuYuiOP3y ago
in this case but it doesn't matter
RiA
RiA3y ago
means you're not targetting the correct controller. either that or your routing is wrong
RiamuYui
RiamuYuiOP3y ago
this is my routing from earlier
RiamuYui
RiamuYuiOP3y ago
and I'm calling that route in the HttpPost and how would I be targeting the wrong controller when I'm in the OSMLatLonController
RiA
RiA3y ago
the wrong route then perhaps. tldr it's supposed to be a json response but you're getting a view instead maybe using swagger will help?
RiamuYui
RiamuYuiOP3y ago
currently am
RiamuYui
RiamuYuiOP3y ago
RiA
RiA3y ago
now compare that vs the route of the method you're expecting your json from
RiA
RiA3y ago
i mean the routing setup of that method
RiamuYui
RiamuYuiOP3y ago
what do you mean? I don't follow
RiA
RiA3y ago
the route attribute in your controller methods the method that delivers the json
RiamuYui
RiamuYuiOP3y ago
as in
RiA
RiA3y ago
yeah that
RiamuYui
RiamuYuiOP3y ago
yeah it seems the same
RiA
RiA3y ago
and what are you returning
RiamuYui
RiamuYuiOP3y ago
I thought i was returning the raw json from the api
RiA
RiA3y ago
what returned the html? PostAsync? or the result of this method
RiamuYui
RiamuYuiOP3y ago
the GetAsync
RiA
RiA3y ago
yes i'm asking you to check the routing of where you're recieving for GetAsync from
RiamuYui
RiamuYuiOP3y ago
is the route not GetAsync($"/reverse?format=json&limit=1&{osmLatLonModel.LLQueryString()}", cancellationToken);
RiA
RiA3y ago
on this end of your method yes check what the other end is doing because if it's not sending you the correct data, then something isn't working as it's supposed to on the other end
RiamuYui
RiamuYuiOP3y ago
by the other end do you mean my LLQueryString?
RiA
RiA3y ago
yes
RiamuYui
RiamuYuiOP3y ago
this seems right too
RiA
RiA3y ago
GetAsync send a request and recieves it async you're getting an html page instead of a json response so wherever you're sending that Get to, isn't sending you the correct data that's what i mean so check the code on that end
RiamuYui
RiamuYuiOP3y ago
it's sending the get to the domain
RiA
RiA3y ago
check the method that handles that get request
RiamuYui
RiamuYuiOP3y ago
the OSMLatLonController is said method is it not?
RiA
RiA3y ago
i dont think so while you're handling a request, you're also making another internal request which is the GetAsync() that's the method that isn't providing the correct data for you, so find out where that data is being sent from and correct it
RiamuYui
RiamuYuiOP3y ago
all the GetAsync() is being sent is my LLQueryString
RiA
RiA3y ago
i thought the html page was what it was sending back
RiamuYui
RiamuYuiOP3y ago
it is, you're correct. and I don't understand
RiA
RiA3y ago
is it supposed to send back an html page? no, right?
RiamuYui
RiamuYuiOP3y ago
no it's meant to send back the json i sent at the beginning hence the confusion
RiA
RiA3y ago
yes. so that means wherever you're calling with the GetAsync() isn't delivering the correct data to you you need to find out what THAT controller is doing. not the one you're in right now
RiamuYui
RiamuYuiOP3y ago
I haven't written another controller unless i'm mistaken I'm only calling the GetAsync in this controller I have a query model, a response model, a definition for locationresult and the controller i guess and the routing
RiA
RiA3y ago
here you're calling GetAsync who handles the request that GetAsync makes?
RiamuYui
RiamuYuiOP3y ago
As in what is the definition of GetAsync?
RiA
RiA3y ago
Surely GetAsync doesn't request from the same method you're in, does it?
RiamuYui
RiamuYuiOP3y ago
Isn't GetAsync requesting the string I put into it so in this case $"/reverse?format=json&limit=1&{osmLatLonModel.LLQueryString()}" and that's it no?
RiA
RiA3y ago
no it's making a request with that string
RiamuYui
RiamuYuiOP3y ago
I really don't understand, GetAsync isn't requesting anything from me but rather the api
RiA
RiA3y ago
yes so the API isn't sending you the correct data find out what the correct API is then
RiamuYui
RiamuYuiOP3y ago
but im using this exact api in postman and in by browser and it works fine I've added a user agent to get it past the not found error
RiA
RiA3y ago
but you're recieving an html page in swagger too isn't it
RiamuYui
RiamuYuiOP3y ago
yes
RiA
RiA3y ago
then check if your postman uri and this one matches
RiamuYui
RiamuYuiOP3y ago
they're identical i copy it from my code, put it in postman and bam it works
RiA
RiA3y ago
i'm not quite sure how postman parses the data so i can't give you a definitive about that
RiamuYui
RiamuYuiOP3y ago
so this is postman
RiA
RiA3y ago
okay that isn't an html page but an http wrapped file that actually says the mimetype is an application/json in which the content is 'null'
RiamuYui
RiamuYuiOP3y ago
oookay so i add the header applicatoin/json? yknow but spelled correctly
RiA
RiA3y ago
are you using a tcpclient instead of an httpclient by any chance?
RiamuYui
RiamuYuiOP3y ago
nope httpclientfactory
RiA
RiA3y ago
do the same request in a browser and see view the source? nevermind i'm being dumb the html data itself is 'serialized' in json when it's being sent to you which means while the json is already null, it's placed into an html page, then the TCP data of the page itself is serialized into a json. something is wrong with either the api you're supposed to use, or postman handles deserialization of the http headers and is able to extract just the content from it
RiamuYui
RiamuYuiOP3y ago
my browser also displays the data no problem
RiA
RiA3y ago
html serialized as json, yes?
RiamuYui
RiamuYuiOP3y ago
I don't know how to decipher that
RiA
RiA3y ago
screenie?
RiamuYui
RiamuYuiOP3y ago
RiA
RiA3y ago
right click > view source
RiamuYui
RiamuYuiOP3y ago
RiA
RiA3y ago
that is certainly odd try deserializing the result from your GetAsync into an HTTPResponse object and try to get the content from it
RiamuYui
RiamuYuiOP3y ago
Right how does one do that
RiA
RiA3y ago
you already have the string so json deserialize to the HttpResponse type
RiA
RiA3y ago
the objects signature is kinda similar to the page you got back might actually work for whatever reason, your api is serializing an httpresponse into a json, i think either that or it's standard procedure to deserialize after GetAsync. idk
RiamuYui
RiamuYuiOP3y ago
okay how does deserializing httpresponses work in this case
RiA
RiA3y ago
<HttpResponse> yeah check the deserialize api description and fill in accordingly mouseover 'deserialize'
RiamuYui
RiamuYuiOP3y ago
what am i missing here
RiA
RiA3y ago
response mouseover response and see why it has a red squiggle under it
RiamuYui
RiamuYuiOP3y ago
and the
RiamuYui
RiamuYuiOP3y ago
but doing so breaks it
RiA
RiA3y ago
?! just mouse over the red squiggle
RiamuYui
RiamuYuiOP3y ago
i did, that's the first screenshot
RiA
RiA3y ago
nevermind didn't see the other half
RiamuYui
RiamuYuiOP3y ago
nw
RiA
RiA3y ago
which jsondeserializer is it System.Text.Json?
RiamuYui
RiamuYuiOP3y ago
yes
RiA
RiA3y ago
wait a sec
Pobiega
Pobiega3y ago
you can't do Deserialize(response);
RiA
RiA3y ago
that looks like you're already getting a HttpResponseMessage
Pobiega
Pobiega3y ago
because response is a HttpResponseMessage you need to get the .Content property out of it and read the content
RiA
RiA3y ago
exactly
Pobiega
Pobiega3y ago
ie, var content = await response.Content.ReadAsStringAsync();
RiamuYui
RiamuYuiOP3y ago
RiamuYui
RiamuYuiOP3y ago
WAIT I DID IT
Pobiega
Pobiega3y ago
your model class you are attempting to deserialize to..
RiamuYui
RiamuYuiOP3y ago
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
I don't, I have finally read the json out of the api, I now need to just set lat, lon and display_name as variables
RiA
RiA3y ago
that html page put me off. i thought it was a string. you should check the type you're getting back. mouse over more often!
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
I appreciate the help nonetheless
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
but while im at it, why isn't this select working 😂
RiamuYui
RiamuYuiOP3y ago
yes?
Pobiega
Pobiega3y ago
because content is a string
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3y ago
.Select over a string will iterate over the individual chars
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiA
RiA3y ago
you'll have to deserialize that content again into your model
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiA
RiA3y ago
HttpResponseMessage.Content extract the actual content from the http response data. then deserialization is upto you
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
Pobiega
Pobiega3y ago
you'll need to set a [JsonConstructor] attribute on the ctor
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
RiamuYui#6195
Quoted by
From RiamuYui#6195
React with ❌ to remove this embed.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3y ago
there is plenty of reason to do that
RiamuYui
RiamuYuiOP3y ago
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiA
RiA3y ago
content is a json string currently
Pobiega
Pobiega3y ago
he doesnt want the string thou, he wants the data
RiA
RiA3y ago
they need to deserialize it into the model first, then they can use Linq on it
RiamuYui
RiamuYuiOP3y ago
okay deserializing the data manually look something like this?
RiamuYui
RiamuYuiOP3y ago
i had this before but it never got to see any use
Pobiega
Pobiega3y ago
your models need to match the json structure exactly
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
as in in order too?
Pobiega
Pobiega3y ago
can you paste the json you got here, in a code block?
RiamuYui
RiamuYuiOP3y ago
like none of the values are nested
RiA
RiA3y ago
i dont think order matters. only the hierarchy
Pobiega
Pobiega3y ago
yeah, hierarchy, types, and names
RiamuYui
RiamuYuiOP3y ago
i can paste the json but how do i do it in a code block
RiA
RiA3y ago
and names yeah
Pobiega
Pobiega3y ago
$code
MODiX
MODiX3y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/ To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3y ago
{
"place_id": 128834247,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "way",
"osm_id": 100490251,
"lat": "51.9988103",
"lon": "-0.9985512",
"display_name": "Tingewick Road Industrial Estate, Buckingham, Buckinghamshire, England, MK18 1FY, United Kingdom",
"address": {
"industrial": "Tingewick Road Industrial Estate",
"town": "Buckingham",
"county": "Buckinghamshire",
"state": "England",
"ISO3166-2-lvl4": "GB-ENG",
"postcode": "MK18 1FY",
"country": "United Kingdom",
"country_code": "gb"
},
"boundingbox": [
"51.9988103",
"51.9996756",
"-0.9985512",
"-0.996438"
]
}
{
"place_id": 128834247,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "way",
"osm_id": 100490251,
"lat": "51.9988103",
"lon": "-0.9985512",
"display_name": "Tingewick Road Industrial Estate, Buckingham, Buckinghamshire, England, MK18 1FY, United Kingdom",
"address": {
"industrial": "Tingewick Road Industrial Estate",
"town": "Buckingham",
"county": "Buckinghamshire",
"state": "England",
"ISO3166-2-lvl4": "GB-ENG",
"postcode": "MK18 1FY",
"country": "United Kingdom",
"country_code": "gb"
},
"boundingbox": [
"51.9988103",
"51.9996756",
"-0.9985512",
"-0.996438"
]
}
RiA
RiA3y ago
well in this case
Pobiega
Pobiega3y ago
and you are after the display_name, lat and lon only?
RiA
RiA3y ago
yeah you can simply dser into that class then and it should hold your values
RiamuYui
RiamuYuiOP3y ago
yes
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3y ago
yep, then get rid of your OSMResponseModel class
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3y ago
you can deserialize directly into OSMOutputs from that at the root level
RiamuYui
RiamuYuiOP3y ago
in this case are the objects lat and lon and such or new
Pobiega
Pobiega3y ago
? they will contain the data from the json thats the entire point 🙂
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3y ago
and because your properties are not readonly ( {get;set;}), you dont need a constructor or any attributes
RiamuYui
RiamuYuiOP3y ago
had this error The JSON value could not be converted to System.Decimal. Path: $.lat | LineNumber: 0 | BytePositionInLine: 152.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3y ago
right, latand lon should not be decimal
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3y ago
change them to strings, you shouldnt treat them as mathematical values
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
AH WONDERFUL
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
good lord that took a hefty amount of learning if you couldn't tell, I've only just started with this
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3y ago
So much ego. The reason I brought up JsonConstructor was because in his previous code, his response model was read only and had a constructor. and thats fine, but then it REQUIRES that attribute to work.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
guys we solved it, it's all good. I don't have to edit them, just read them from elsewhere in the code speaking of, if I'm returning osmOutputs, can I read each value individually
Pobiega
Pobiega3y ago
Good design would be using a record for your response model. wdym by "read each value"?
RiamuYui
RiamuYuiOP3y ago
I still have that responsemodel
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
as in currently they're one long string right? and I want to assign them to variables elsewhere
Pobiega
Pobiega3y ago
you are returning an instance of an object with the values set to whatever the API responded with they are not a string
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
can i pull them out with the LocationResult?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
ohhh okay
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
RiamuYui
RiamuYuiOP3y ago
yup it's working thanks so much for the help
Want results from more Discord servers?
Add your server