Possible values of empty json

An empty string is not a valid json;

string json = "";

While an empty string is not valid JSON two quotes is valid JSON. This is an important distinction.

Which is to say a string that contains two quotes is not the same thing as an empty string.

string json = "{}";
string json = "[]";

Valid minimal JSON strings are

The empty object '{}'

The empty array '[]'

The string that is empty '""'

A number e.g. '123.4'

The boolean value true 'true'

The boolean value false 'false'

The null value 'null'

Resource

https://stackoverflow.com/questions/30621802/why-does-json-parse-fail-with-the-empty-string

Convert Json String to C# object

Let’s say we have this json string in Ajax post method;

let groupObject = '[{ "Key": "Commercial", "IsMember": "true" }, { "Key": "Corporate", "IsMember": "false" }, { "Key": "Consumer", "IsMember": "false" }]';

Make sure this is a valid json. We can use following web site for json validation;

https://jsonlint.com/

We will create a user group class in .NET;

public class UGroupVM
{
    public string? Key { get; set; }
    public string? IsMember { get; set; }
}

We will user JSON Deserialization to get group object (try to use IList or List otherwise deserialization will fail because json is formatted as key/value pair);

var userGroup = JsonSerializer.Deserialize<List<UserGroupVM>>(groupObject);

For Newtonsoft, this is the syntax

var userAdGroup =
 JsonConvert.DeserializeObject<List<UserGroupVM>>(groupObject);

An alternative is this;

JArray jarray = JArray.Parse(groupObject);

foreach (JObject jObject in jArray)
{
    Console.WriteLine($"{(string)jObject["Key"]} -> {(string)jObject["IsMember"]}");
}

Ajax request other than Get and Post

Type setting in Ajax request is used to specify HTTP method that can be used to make remote calls. Many firewalls and application servers are configured to discard other kind of request except Get and Post.

If we want to use other HTTP methods, then we can make a POST request, but add the X-HTTP-Method-Override header; Setting it to the method we want to use, like this;

X-HTTP-Method-Override: PUT

This convention is widely supported by web application frameworks and is a common way of creating RESTful web applications.

You can learn more about this here;

https://en.wikipedia.org/wiki/Representational_state_transfer

See the “Setting Timeouts and Headers” section for detail of how to set a header on a jQuery Ajax request.

Throw exception on server side and handle in jQuery AJAX post call

XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing.

Here are some references;

https://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages

Calculate your household energy usage in kWh

You need to know the wattage and electric rate per kWh. For me the electric rate is (13 cents) .13 per kWh. Let’s find out how to get wattage.

How can I find the wattage of a device?

Most devices have a label listing how many watts they use. You can find this wattage label either on the device (usually on the bottom or back) or in the owner’s manual.

Here is a list that shows the common wattage of everyday household devices. Though the wattage of your particular device may vary, it should give you a rough estimate.

Coffee Maker900-1200 watts
Toaster800-1400 watts
Iron100-1800 watts
Ceiling fan65-175 watts
Space heater (40gal)4500-5500 watts
Hair dryer1200-1875 watts
Laptop50 watts
Computer monitor150 watts
Computer tower120 watts
Television 19″-36″65-133 watts
Television 53″-61″170 watts
How to calculate kWh usage

To calculate your energy consumption, you’ll need to multiply an appliance’s wattage by the number of hours you use it in a day. That will give you the number of watt-hours consumed each day.

Calculate watt-hours per day

Device Wattage (watts) x Hours Used Per Day = Watt-hours (Wh) per Day
Example: A 125-watt television used three hours per day
125 watts x 3 hours = 375 Wh/Day

How many watts are in a kilowatt?

Your electricity bill is measured in kilowatt-hours (kWh), not watt-hours. One kilowatt is equal to 1,000 watts. To calculate how many kWh a device uses, divide the watt-hours from the previous step by 1,000.

Convert watt-hours to kilowatts

Device Usage (Wh) / 1000 (Wh/kWh) = Device Usage in kWh
Example: A television using 375 Wh of electricity per day
375 / 1000 = 0.375 kWh

Now that we know how many kWh the appliance uses per day, we can estimate that usage over a month. Let’s multiply by 30 days to simulate an average month.

Find your monthly energy usage

Daily Usage (kWh) x 30 (Days) = Approximate Monthly Usage (kWh/Month)
Example: A television using 0.375 kWh of electricity per day
0.375 kWh x 30 Days = 11.25 kWh/Month

In this example, a 125-watt television you use for three hours per day adds up to 11.25 kWh of energy per month. That is your television’s energy consumption.

To determine how much your appliances cost per month, multiply your electric rate by the estimated monthly usage from the steps above.

How much do appliances cost on my energy bills?

Monthly Usage (kWh) x Electric Rate ($/kWh) = Approximate Cost per Month
Example: A television using 11.25 kWh/Month with an electric rate of 10 cents per kWh ($0.10/kWh)
11.25 kWh x $0.10 = $1.13/Month

Based on these calculations, this television would cost you $1.13 per month. While that might not seem like much, the appliances and devices throughout your home will add up during a full month.

Your UPS would be another source of getting wattage. UPS will show the wattage number of all the devices that are connected. You can use that number to calculate your spending.

Reference

https://www.saveonenergy.com/resources/energy-consumption/