Deserialize RESTSharp JSON Response - c#

I'm working on a project to gather data from NOAA. I'm having trouble figuring out how to make the response usable.
This is what NOAA's API response looks like to my call:
{
"metadata": {
"resultset": {
"offset": 1,
"count": 38859,
"limit": 2
}
},
"results": [
{
"mindate": "1983-01-01",
"maxdate": "2019-12-24",
"name": "Abu Dhabi, AE",
"datacoverage": 1,
"id": "CITY:AE000001"
},
{
"mindate": "1944-03-01",
"maxdate": "2019-12-24",
"name": "Ajman, AE",
"datacoverage": 0.9991,
"id": "CITY:AE000002"
}
]
}
I used JSON2CSharp.com to convert the result set into my needed classes. Below is the relevant code:
public class NOAA
{
public class Resultset
{
public int offset { get; set; }
public int count { get; set; }
public int limit { get; set; }
}
public class Metadata
{
public Resultset resultset { get; set; }
}
public class Location
{
public string mindate { get; set; }
public string maxdate { get; set; }
public string name { get; set; }
public double datacoverage { get; set; }
public string id { get; set; }
}
public class RootObject
{
public Metadata metadata { get; set; }
public List<Location> results { get; set; }
}
public class Response
{
IList<Metadata> metadata;
IList<Location> results;
}
public void RestFactory(string Token, string Endpoint, Dictionary<string, string> Params)
{
// Initiate the REST request
var client = new RestClient("https://www.ncdc.noaa.gov/cdo-web/api/v2/" + Endpoint);
var request = new RestRequest(Method.GET);
// Add the token
request.AddHeader("token", Token);
// Add the parameters
foreach (KeyValuePair<string, string> entry in Params)
{
request.AddParameter(entry.Key, entry.Value);
}
// Execute the REST request
var response = client.Execute(request);
// Deserialize the response
Response noaa = new JsonDeserializer().Deserialize<Response>(response);
// Print to console
foreach (Location loc in noaa)
{
Console.WriteLine(loc.name);
}
}
}
At this point, I'm just trying to print the location name to reach my next learning milestone. I'm getting the error:
Severity Code Description Project File Line Suppression State
Error CS1579 foreach statement cannot operate on variables of type 'NOAA.Response' because 'NOAA.Response' does not contain a public instance definition for 'GetEnumerator'
Other than the error, I think I don't quite understand the proper approach since the response has more than one "layer". Guidance?

Your foreach loop is trying to call an iterator on the object itself, not the list inside it.
Try this instead
foreach (Location loc in noaa.results)
{
Console.WriteLine(loc.name);
}

I can tell you the cause of error. That is because noaa is not iterable. If you want to iterate over any object then it needs to implement IEnumerable interface. This is the reason because of which noaa is not iterable. noaa does not inherit this interface or implement it. Do you get the same error if you use noaa.results?

Related

Access JSON keys in C# from ServiceNow Rest API

I am calling the ServiceNow Incidents table and pulling back one incident like this. https://mydevInstance.service-now.com/api/now/v1/table/incident?sysparm_limit=1
var client = new RestClient("https://mydevInstance.service-now.com/api/now/v1/table/incident?sysparm_limit=1");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Basic myAuthKey");
IRestResponse response = client.Execute(request);
The JSON it returns in RESTSharp looks like this.
{
"result": [
{
"parent": "",
"made_sla": "true",
"caused_by": "",
"watch_list": "",
"upon_reject": "cancel",
"resolved_by": {
"link": "https://mydevInstance.service-now.com/api/now/v1/table/sys_user/5137153cc611227c000bbd1bd8cd2007",
"value": "5137153cc611227c000bbd1bd8cd2007"
},
"approval_history": "",
"number": "INC0000060"
}
]
}
How do I create a C# list or array of all the Keys under result? I can't Serialize the object with JSON.Net because additional keys can be added over time.
You need to grab the sample of the JSON content, then make a C# class using the 'Paste Special' option I described.
Then you can use the JsonConvert.DeserializeObject<T> (in a nuget package by Newtonsoft) to deserialize your web service response in a C# object instance.
Here are the C# classes I generated with your JSON object unaltered:
public class Rootobject
{
public Result[] result { get; set; }
}
public class Result
{
public string parent { get; set; }
public string made_sla { get; set; }
public string caused_by { get; set; }
public string watch_list { get; set; }
public string upon_reject { get; set; }
public Resolved_By resolved_by { get; set; }
public string approval_history { get; set; }
public string number { get; set; }
}
public class Resolved_By
{
public string link { get; set; }
public string value { get; set; }
}
You use this type like this:
var json = "t-b-d"; // From Web Service call
Rootobject response = JsonConvert.DeserializeObject<Rootobject>(json);
// use the response object.
** UPDATED **
If you need a more flexible model, all JSON will deserialize into Dictionary<string, string>, but I have found that serialization / deserialization results are more reliable when the model is consistent
var response = JsonConvert.DeserializeObject<Dictionary<string,string>>(json);
Here is what does work using System.Text.Json
var incidentFields = new List<string>();
var doc = JsonDocument.Parse(json);
foreach (var o in doc.RootElement.GetProperty("result").EnumerateArray())
{
foreach (var p in o.EnumerateObject())
{
incidentFields.Add(p.Name.ToString());
}
}
I created a library that handles that by default. (You can add custom types also)
https://autodati.github.io/ServiceNow.Core/

how to pull out array of string values from a deserialized JSON response

I am pretty new to coding and here is my requirement:
I am getting a JSON response which has an array of values (refer read,update,delete in the below JSON response)
The number of values is dynamic and tend to vary each time.
I want to know how to retrieve them and put into an string array and return the values
Eg.: end result should be like
string[] deleteValues = {"MCS:Menu:Admin","MCS:test"}
In case if there is answer already available to this question, please point me in the right direction.
Thanks in advance
==========================================
I am able to get the values this way...
string value1 = new JavaScriptSerializer().Deserialize<JSON_Deconstructor>(resp).Permitted[0].Delete[0].ToString();
string value2 = new JavaScriptSerializer().Deserialize<JSON_Deconstructor>(resp).Permitted[0].Delete[1].ToString();
but since the number of values in delete is dynamic, i need to how to pull them.
====================
the code snippet:
string resp = new StreamReader(request1.GetResponse().GetResponseStream()).ReadToEnd(); // resp is a JSON response from a server
JSON_Deconstructor dc = new JSON_Deconstructor { };
dc.Permitted = new Permitted[1];
string value1 = new JavaScriptSerializer().Deserialize<JSON_Deconstructor>(resp).Permitted[0].Delete[0].ToString();
string value2 = new JavaScriptSerializer().Deserialize<JSON_Deconstructor>(resp).Permitted[0].Delete[1].ToString();
==================
JSON_Deconstructor class contents:
public class JSON_Deconstructor
{
public Permitted[] Permitted { get; set; }
public Denied[] Denied { get; set; }
}
==================
Permitted class contents:
public class Permitted
{
public string[] Read { get; set; }
public string[] Update { get; set; }
public string[] Delete { get; set; }
}
=================
JSON response:
{
"Permitted": [
{
"read": [
"MCS:Menu:Admin"
],
"update": [
"MCS:test"
],
"delete": [
"MCS:Menu:Admin",
"MCS:test"
]
}
]
}
First add jsonProperty to your class in order to be able to serialize.
public class Permitted
{
[JsonProperty("read")]
public string[] Read { get; set; }
[JsonProperty("update")]
public string[] Update { get; set; }
[JsonProperty("delete")]
public string[] Delete { get; set; }
}
//Response contains a list of permitted objects in Permitted property
public class PermittedResponse
{
public List<Permitted> Permitted { get; set; }
}
then in you method de serialize your response and loop through results to build your arrays.
List<string> deletedValues = new List<string>();
List<string> readValues = new List<string>();
List<string> updateValues = new List<string>();
PermittedResponse response = JsonConvert.DeserializeObject<PermittedResponse>(serializedJson);
response.Permitted.ForEach(e =>
{
deletedValues = deletedValues.Concat(e.Delete).ToList();
readValues = readValues.Concat(e.Read).ToList();
updateValues = updateValues.Concat(e.Update ).ToList();
});
Use Newtonsoft.Json. You can get it from NuGet. This is very simple and powerful library for Json.
Now, you should create a class, like:
public class Item
{
public List<string> MCS { get; set; } = new List<string>();
}
public class PermitedItem
{
public Item read {get; set;}
public Item update {get; set;}
public Item delete {get; set;}
}
public class MyResponse
{
public List<PermittedItem> Permitted = new List<PermittedItems>();
}
And then you use it like that:
MyResponse result = JsonConvert.DeserializeObject<MyResponse>(jsonAsString);
This should work.

C# Json how to deserialize something with a key that could be any kind of int

I'm working on a Xamarin App, and I use Newtonsoft for Json.
But I'm having trouble with processing some data that I get back.
{
"ok": true,
"payment-methods": [
{
"id": "39sahf92ka9s02",
"type": "ideal",
"options": {
"issuers": {
99: "Test Issuer"
}
}
}
],
}
I don't know how to get to the Test Issuer, because the Key value could be any integer.
A Dictionary makes a lot of sense to use, but then I get the following exception: "System.NullReferenceException: Object reference not set to an instance of an object. json"
I have the following as Model:
[JsonObject(MemberSerialization.OptIn)]
public class PaymentOptions
{
[JsonProperty("ok")]
public Boolean OK { get; set; }
[JsonProperty("payment-methods")]
public List<PaymentMethods> PaymentMethods { get; set; }
}
public class PaymentMethods
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("options")]
public Options Options { get; set; }
}
public class Options
{
[JsonProperty("issuers")]
public IDictionary<int, string> Issuers { get; set; }
}
I deserialize the Json through the following:
var deserializedGetPaymentOptions = JsonConvert.DeserializeObject<Models.PaymentMethods>(await responseGetPaymentOptions.Content.ReadAsStringAsync());
And after that I try to read it by using it in a foreach loop:
foreach (KeyValuePair<int, string> issuerFromDict in deserializedGetPaymentOptions.Options.Issuers)
Have you tried with 99 as string in the json representation ?
...
"issuers": {
"99": "Test Issuer"
}
...
You should let newtonsoft deals with the conversion to int.
I fixed it by doing the following:
var deserializedGetPaymentOptions = JsonConvert.DeserializeObject<Models.PaymentOptions>(await responseGetPaymentOptions.Content.ReadAsStringAsync());
foreach (var desPayOp in deserializedGetPaymentOptions.PaymentMethods) {
Debug.WriteLine("start foreach");
foreach (KeyValuePair<int, string> issuerFromDict in desPayOp.Options.Issuers)
{
Debug.WriteLine(issuerFromDict.Key.ToString() + " : " + issuerFromDict.Value);
}
}
I now deserialize it from PaymentOptions instead of PaymentMethods, then loop through the List and after that through the dictionary.

NEST Api SearchAfter return null in NEST but works in Kibana

We are using elastic search just for document search in our application so we don't have any one expert in it. I was able to use TermQuery, SimpleQueryStringQuery and MatchPhraseQuery successfully. But I found out in documentation that using From & Size for pagination is not good for production and Search After is recommended.
But my implementation return null. It is confusing for me what should be in <Project> parameter as shown in Nest API Object Initializer Syntax in docs here.
My code looks like this:
var request = new SearchRequest<ElasticSearchJsonObject._Source>
{
//Sort = new List<ISort>
//{
// new SortField { Field = Field<ElasticSearchJsonObject>(p=>)}
//},
SearchAfter = new List<object> {
},
Size = 20,
Query = query
};
Reality is I don't understand this. Over here ElasticSearchJsonObject._Source is the class to map returned results.
My documents are simple text documents and I only want documents sorted according to score so document Id is not relevant.
There was already a question like this on SO but I can't find it somehow.
Update
After looking at answer I updated my code and though query obtained does work. It return result in kibana but not in NEST.
This is the new updated code:
var request = new SearchRequest<ElasticSearchJsonObject.Rootobject>
{
Sort = new List<ISort>
{
new SortField { Field = "_id", Order = SortOrder.Descending}
},
SearchAfter = new List<object> {
"0fc3ccb625f5d95b973ce1462b9f7"
},
Size = 1,
Query = query
};
Over here I am using size=1 just for test as well as hard code _id value in SearchAfter.
The query generated by NEST is:
{
"size": 1,
"sort": [
{
"_id": {
"order": "desc"
}
}
],
"search_after": [
"0fc3ccb625f5d95b973ce1462b9f7"
],
"query": {
"match": {
"content": {
"query": "lahore",
"fuzziness": "AUTO",
"prefix_length": 3,
"max_expansions": 10
}
}
}
}
The response from the ES does say successful but no results are returned.
Results do return in Kibana
Query status is successful
But...
Total returned is 0 in NEST
Sort value is null in kibana I used TrackScores = true to solve this issue
Here is the debug information:
Valid NEST response built from a successful low level call on POST: /extract/_source/_search?typed_keys=true
# Audit trail of this API call:
- [1] HealthyResponse: Node: http://localhost:9200/ Took: 00:00:00.1002662
# Request:
{"size":1,"sort":[{"_id":{"order":"desc"}}],"search_after":["0fc3ccb625f5d95b973ce1462b9f7"],"query":{"match":{"content":{"query":"lahore","fuzziness":"AUTO","prefix_length":3,"max_expansions":10}}}}
# Response:
{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
So please tell me where I am wrong and what can be the problem and how to solve it.
Update 2:
Code in Controller:
Connection String:
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
settings.DisableDirectStreaming();
settings.DefaultIndex("extract");
var client = new ElasticClient(settings);
Query:
var query = (dynamic)null;
query = new MatchQuery
{
Field = "content",
Query = content,
Fuzziness = Fuzziness.Auto,
PrefixLength = 3,
MaxExpansions = 10
};
Query Builder
var request = new SearchRequest<ElasticSearchJsonObject.Rootobject>
{
Sort = new List<ISort>
{
new SortField { Field = "_id", Order = SortOrder.Descending}
},
SearchAfter = new List<object> {
documentid //sent as parameter
},
Size = 1, //for testing 1 other wise 10
TrackScores = true,
Query = query
};
JSON Query
I use this code to get query I posted above. This query is then passed to kibana with GET <my index name>/_Search and there it works
var stream = new System.IO.MemoryStream();
client.SourceSerializer.Serialize(request, stream);
var jsonQuery = System.Text.Encoding.UTF8.GetString(stream.ToArray());
ES Response
string responseJson = "";
ElasticSearchJsonObject.Rootobject response = new ElasticSearchJsonObject.Rootobject();
var res = client.Search<object>(request);
if (res.ApiCall.ResponseBodyInBytes != null)
{
responseJson = System.Text.Encoding.UTF8.GetString(res.ApiCall.ResponseBodyInBytes);
try
{
response = JsonConvert.DeserializeObject<ElasticSearchJsonObject.Rootobject>(responseJson);
}
catch (Exception)
{
var model1 = new LoginSignUpViewModel();
return PartialView("_NoResultPage", model1);
}
}
This is where things go wrong. Above debug information was captured from response
ElasticSearchJsonObject
Some how I think problem might be here somewhere? The class is generated by taking response from NEST in Search request.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ESAPI
{
public class ElasticSearchJsonObject
{
public class Rootobject
{
public int took { get; set; }
public bool timed_out { get; set; }
public _Shards _shards { get; set; }
public Hits hits { get; set; }
}
public class _Shards
{
public int total { get; set; }
public int successful { get; set; }
public int skipped { get; set; }
public int failed { get; set; }
}
public class Hits
{
public int total { get; set; }
public float max_score { get; set; }
public Hit[] hits { get; set; }
}
public class Hit
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public float _score { get; set; }
public _Source _source { get; set; }
}
public class _Source
{
public string content { get; set; }
public Meta meta { get; set; }
public File file { get; set; }
public Path path { get; set; }
}
public class Meta
{
public string title { get; set; }
public Raw raw { get; set; }
}
public class Raw
{
public string XParsedBy { get; set; }
public string Originator { get; set; }
public string dctitle { get; set; }
public string ContentEncoding { get; set; }
public string ContentTypeHint { get; set; }
public string resourceName { get; set; }
public string ProgId { get; set; }
public string title { get; set; }
public string ContentType { get; set; }
public string Generator { get; set; }
}
public class File
{
public string extension { get; set; }
public string content_type { get; set; }
public DateTime last_modified { get; set; }
public DateTime indexing_date { get; set; }
public int filesize { get; set; }
public string filename { get; set; }
public string url { get; set; }
}
public class Path
{
public string root { get; set; }
public string _virtual { get; set; }
public string real { get; set; }
}
}
}
I am sure this can be used to get response.
Please note that in case of simple search this code works:
so for this query below my code is working:
var request = new SearchRequest
{
From = 0,
Size = 20,
Query = query
};
Using from/size is not recommended for deep pagination because of the amount of documents that need to be fetched from all shards for a deep page, only to be discarded when finally returning an overall ordered result set. This operation is inherent to the distributed nature of Elasticsearch, and is common to many distributed systems in relation to deep pagination.
With search_after, you can paginate forward through documents in a stateless fashion and it requires
the documents returned from the first search response are sorted (documents are sorted by _score by default)
passing the values for the sort fields of the last document in the hits from one search request as the values for "search_after": [] for the next request.
In the Search After Usage documentation, a search request is made with sort on NumberOfCommits descending, then by Name descending. The values to use for each of these sort fields are passed in SearchAfter(...) and are the values of Project.First.NumberOfCommits and Project.First.Name properties, respectively. This tells Elasticsearch to return documents that have values for the sort fields that correspond to the sort constraints for each field, and relate to the values supplied in the request. For example, sort descending on NumberOfCommits with a supplied value of 775 means that Elasticsearch should only consider documents with a value less than 775 (and to do this for all sort fields and supplied values).
If you ever need to dig further into any NEST documentation, click the "EDIT" link on the page:
which will take you to the github repository of the documentation, with the original asciidoc markdown for the page:
Within that page will be a link back to the original NEST source code from which the asciidoc was generated. In this case, the original file is SearchAfterUsageTests.cs in the 6.x branch

Download and Parse Json File Server Side

I am learning for the first time how to get a json file from a third party vendor and I am trying to do such with steam. I am trying to retrieve game name and play time of a specific game for a specific user. Based on the online documentation I have read the following code should be working, but the problem is that I am getting back a null. If I take the generated URL and put it in the browser I get back results which means my URL is good, but the way I am parsing it is wrong.
public class SteamMemberViewModel
{
public List<SteamGameViewModel> games { get; set; }
}
public class SteamGameViewModel
{
public int appid { get; set; }
public string name { get; set; }
public int playtime_forever { get; set; }
}
private string GetSteamGame()
{
const int rocketLeagueId = 252950;
var format = string.Format("http://api.steampowered.com/{0}/{1}/v{2}/?key={3}&steamid={4}&include_appinfo=1&format=json", "IPlayerService", "GetOwnedGames", "0001", "ABC", "123");
using (WebClient wc = new WebClient())
{
var json = JsonConvert.DeserializeObject<SteamMemberViewModel>(wc.DownloadString(format));
var rocketLeage = json.games.Where(g => g.appid == rocketLeagueId);
var steamGameViewModels = rocketLeage as SteamGameViewModel[] ?? rocketLeage.ToArray();
if (steamGameViewModels.Count() == 1)
{
var playtime = steamGameViewModels.First().playtime_forever;
return steamGameViewModels.First().name + " - " + playtime;
}
}
return "Steam Game Not Found";
}
The error that I Get is
Value cannot be null.
Parameter name: source
Line 26: var json = JsonConvert.DeserializeObject(wc.DownloadString(format));
Line 27:
Line 28: var rocketLeage = json.games.Where(g => g.appid == rocketLeagueId);
Line 29: var steamGameViewModels = rocketLeage as SteamGameViewModel[] ?? rocketLeage.ToArray();
Line 30: if (steamGameViewModels.Count() == 1)
Source File: e:_websites\Local\Projects\Azularis\Azularis.System.Events\Azularis.System.Events\Controllers\HomeController.cs Line: 28
EDIT:
I have also tried running the code as follows:
var result = wc.DownloadString(format);
var data = JsonConvert.DeserializeObject<SteamMemberViewModel>(result);
var count = data.games.Count();
return count.ToString();
And I still got the same error. result comes back with values though.
JSON FILE EXAMPLE:
{
"response": {
"game_count": 16,
"games": [
{
"appid": 10,
"name": "Counter-Strike",
"playtime_forever": 5019,
"img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5",
"img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f",
"has_community_visible_stats": true
}
]
}
}
Value games inside SteamMemberViewModel is always null.
I would think your issue is that your top object in the JSON is response but you are trying to parse that into the equivalent of the games array. I would think you need to get the array object and then parse so you need to go down a level into the JSON object.
I can't be sure as you do not know if that serializer is performing some recursive work until it finds the object (I doubt it though) but the docs does not seem to do so:
http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm
in the example, it clearly states you need to get down to the object you mean to be your top level. And it is not response in your example.
So you'd have to do something like (pseudo-code):
JObject json = JObject.Parse(jsonString);
IList<JToken> array = json["response"]["games"].Children().ToList();
if(array != null)
{
var data = JsonConvert.DeserializeObject<SteamMemberViewModel>(array.ToString());
}
I know you already have an accepted answer. Your issue is easily fixable. Replace SteamMemberViewModel code with below code.
public class SteamMemberViewModel
{
public Response response { get; set; }
}
public class Response
{
public int game_count { get; set; }
public Game[] games { get; set; }
}
public class Game
{
public int appid { get; set; }
public string name { get; set; }
public int playtime_forever { get; set; }
public string img_icon_url { get; set; }
public string img_logo_url { get; set; }
public bool has_community_visible_stats { get; set; }
}

Categories