So I am using this myMarketNews api: https://mymarketnews.ams.usda.gov/mars-api/getting-started
and I am able to pull the reports list themselves with the following code:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Authenticators;
namespace MarketNewsJson
{
internal static class MarketNewsJson
{
private static void Main(string[] args)
{
var client = new RestClient("https://marsapi.ams.usda.gov")
{
Authenticator = new HttpBasicAuthenticator("mars_test_343343", "")
};
var request = new RestRequest("services/v1.1/reports", DataFormat.Json);
var response = client.Get(request);
var reports = JsonConvert.DeserializeObject<List<Report>>(response.Content);
foreach (var report in reports)
{
Console.WriteLine($"{report.SlugId} {report.SlugName} - {report.ReportTitle}");
}
}
}
public class Report
{
[JsonProperty("slug_id")]
public string SlugId { get; set; }
[JsonProperty("slug_name")]
public string SlugName { get; set; }
[JsonProperty("report_title")]
public string ReportTitle { get; set; }
[JsonProperty("published_date")]
public string PublishedDate { get; set; }
[JsonProperty("markets")]
public List<string> Markets { get; set; }
[JsonProperty("market_types")]
public List<string> MarketTypes { get; set; }
[JsonProperty("offices")]
public List<string> Offices { get; set; }
[JsonProperty("sectionNames")]
public List<string> SectionNames { get; set; }
}
}
This will pull the report names and their other attributes. However, I also want to be able to pull the JSON information for individual reports themselves. I noticed in the "sorting" tab it shows the properties of an individual report and that for specifying an individual report, it would be something like var request = new RestRequest("services/v1.1/reports/1095", DataFormat.Json); for report 1095 itself instead of /reports for all reports. When I do this, I get an error for deserializing:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MarketNewsJson.Report]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
Is this because the format of the JSON for an individual report is different than the format of the reports list itself? If so, will I need a new line to deserialize it for individual reports and just have the other commented out when I want to grab an individual report and its properties?
EDIT: JSON Response
This is what the JSON response is like for my current just pulling report names, id, and title (which works fine):
[
{
"slug_id": "1034",
"slug_name": "MD_DA105",
"report_title": "Whey - Western & Eastern Europe Report",
"published_date": "07/30/2020 08:25:01",
"markets": ["N/A"],
"market_types": ["Point of Sale"],
"offices": ["Madison"],
"sectionNames": []
}, {
"slug_id": "1035",
"slug_name": "MD_DA106",
"report_title": "Skim Milk Powder - Europe",
"published_date": "07/30/2020 08:25:01",
"markets": ["N/A"],
"market_types": ["Point of Sale"],
"offices": ["Madison"],
"sectionNames": []
}
]
and from the documentation, this is what the individual data for a report is supposed to look like:
{
"results": [
{
"report_begin_date": "2018-01-08",
"report_end_date": "2018-01-08",
"published_date": "2018-01-31",
"office_name": "Madison",
"office_code": "DA-MD",
"office_city": "Madison",
"office_state": "WI",
"market_location_name": "National Cold Storage",
"market_location_city": "",
"market_location_state": "WI",
"group": null,
"category": "Hard Products",
"commodity": "Cheese",
"market_type": "Cold Storage",
"market_type_category": "Dairy Market",
"slug_id": "1095",
"slug_name": "MD_DA953",
"community": "Dairy",
"quality": "N/A",
"holdings_unit": "LBS",
"holdings_current_lbs": "96009227",
"holdings_1stDayMTH_lbs": "96245049",
"holdings_change_lbs": "-235822",
"holdings_change_percent": "0.0000",
"currentMTH_1stDay": "2018-01-01",
"report_narrative": null,
"commodity_narrative": null,
"special_announcement": null
}