1

This is my first post.

I am trying to integrate Shippo to our application and wish to obtain a specific value out of a Json response.

I am getting the following Json response:

{
  "count": 3,
  "next": null,
  "previous": null,
  "results": [
    {
      "object_created": "2019-08-28T12:58:57.064Z",
      "object_id": "16b602e0ajdsk87c4313920bc5e3174XYZ",
      "object_owner": "some@email.com",
      "shipment": "bd62234e151244dab2b2152fdcd15e76",
      "attributes": [
        "FASTEST"
      ],
      "amount": "31.30",
      "currency": "USD",
      "amount_local": "31.30",
      "currency_local": "USD",
      "provider": "USPS",
      "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
      "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
      "servicelevel": {
        "name": "Priority Mail Express",
        "token": "usps_priority_express",
        "terms": ""
      },
      "estimated_days": 1,
      "arrives_by": null,
      "duration_terms": "Overnight delivery to most U.S. locations.",
      "messages": [],
      "carrier_account": "4e1506b8b7f7449e90620967e45aa1e9",
      "test": false,
      "zone": "4"
    },
    {
      "object_created": "2019-08-28T12:58:57.063Z",
      "object_id": "ebdee42047aa49a3b7e08b1903ea02ea",
      "object_owner": "some@email.com",
      "shipment": "bd62234e151244dab2b2152fdcd15e76",
      "attributes": [
        "BESTVALUE",
        "CHEAPEST"
      ],
      "amount": "7.49",
      "currency": "USD",
      "amount_local": "7.49",
      "currency_local": "USD",
      "provider": "USPS",
      "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
      "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
      "servicelevel": {
        "name": "Priority Mail",
        "token": "usps_priority",
        "terms": ""
      },
      "estimated_days": 2,
      "arrives_by": null,
      "duration_terms": "Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.",
      "messages": [],
      "carrier_account": "4e1506b8b7f7449e90620967e45aa1e9",
      "test": false,
      "zone": "4"
    },
    {
      "object_created": "2019-08-28T12:58:57.062Z",
      "object_id": "ad410a41c84940ee80eb30c41c507613",
      "object_owner": "some@email.com",
      "shipment": "bd62234e151244dab2b2152fdcd15e76",
      "attributes": [],
      "amount": "7.78",
      "currency": "USD",
      "amount_local": "7.78",
      "currency_local": "USD",
      "provider": "USPS",
      "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
      "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
      "servicelevel": {
        "name": "Parcel Select",
        "token": "usps_parcel_select",
        "terms": ""
      },
      "estimated_days": 7,
      "arrives_by": null,
      "duration_terms": "Delivery in 2 to 8 days.",
      "messages": [],
      "carrier_account": "4e1506b8b7f7449e90620967e45aa1e9",
      "test": false,
      "zone": "4"
    }
  ]
}

I am using this call:

WebClient webClient = new WebClient();

webClient.Headers.Add("Authorization: ShippoToken " + authToken);

var result = JsonValue.Parse(webClient.DownloadString("https://api.goshippo.com/shipments/"+ theObject.ObjectId + "/rates/USD"));

My question is how could I get just the "amount" value from the response?

Thank you for any help!

canon
  • 40,609
  • 10
  • 73
  • 97

3 Answers3

3

Instead of recreating the json layout as a class model you can work with Newtonsofts JToken. Which will leave you with less boilerplate code to create:

var json = "your json data";

// Parse the whole json string
var obj = JObject.Parse(json);
// Extract the results data as JArray
var results = (JArray)obj["results"];

// Iterate over all array entries and get the amount property
foreach(var result in results)
{
    var amount = result["amount"].ToString();
    // Do something with the amount
}
croxy
  • 4,082
  • 9
  • 28
  • 46
1

You could create some classes based on json then deserialize using JsonConvert e.g https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

tip: Copy the json and put in json viewer like https://jsonformatter.curiousconcept.com/

Alelho
  • 19
  • 1
0

Use https://www.newtonsoft.com/json to deserialize the json response. If you didn't want that you could use a series of string.Split()'s to get the amount. But using newtonsoft (via the nuget package manager) would be the easiest.

Josie G. Bigler
  • 367
  • 4
  • 14