How to take the price from this Json object? - arrays

I tried to get the price value from this json object but no chance ,
can someone help me , thanks
{
"Id":10069,
"UrlHash":"3963aa68aac23b61ffc1275ad6e0f43d",
"BrandId":1,
"Name":"Nokia 8.3 5G",
"Picture":"https://fdn2.gsmarena.com/vv/bigpic/nokia-83-5g.jpg",
"ReleasedAt":"Released 2020, September 15",
"Body":"220g, 9mm thickness",
"Os":"Android 10, up to Android 11, Android One",
"Storage":"64GB/128GB storage, microSDXC",
"DisplaySize":"6.81\\\"",
"DisplayResolution":"1080x2400 pixels",
"CameraPixels":"64 MP ",
"VideoPixels":"2160p",
"Ram":"6/8 GB RAM ",
"Chipset":"Snapdragon 765G 5G",
"BatterySize":"4500 mAh ",
"BatteryType":"Li-Po",
"Specifications":"{\\\"Technology\\\":\\\"GSM \\\\/ HSPA \\\\/ LTE \\\\/ 5G\\\",\\\"2G bands\\\":\\\"GSM 850 \\\\/ 900 \\\\/ 1800 \\\\/ 1900 - SIM 1 & SIM 2\\\",\\\"3G bands\\\":\\\"HSDPA 850 \\\\/ 900 \\\\/ 1700(AWS) \\\\/ 1900 \\\\/ 2100 \\\",\\\"4G bands\\\":\\\"1, 2, 3, 4, 5, 7, 8, 12, 13, 17, 20, 28, 32, 38, 39, 40, 41, 66, 71\\\",\\\"5G bands\\\":\\\"1, 2, 3, 5, 7, 8, 28, 38, 40, 41, 66, 71, 78 SA\\\\/NSA\\\",\\\"Speed\\\":\\\"HSPA 42.2\\\\/5.76 Mbps, LTE-A (4CA) Cat18 1200\\\\/150 Mbps, 5G 2.4\\\\/1.2 Gbps\\\",\\\"Announced\\\":\\\"2020, March 19\\\",\\\"Status\\\":\\\"Available. Released 2020, September 15\\\",\\\"Dimensions\\\":\\\"171.9 x 78.6 x 9 mm (6.77 x 3.09 x 0.35 in)\\\",\\\"Weight\\\":\\\"220 g (7.76 oz)\\\",\\\"SIM\\\":\\\"Single SIM (Nano-SIM) or Hybrid Dual SIM (Nano-SIM, dual stand-by)\\\",\\\"Type\\\":\\\"Li-Po 4500 mAh, non-removable\\\",\\\"Size\\\":\\\"6.81 inches, 112.0 cm2 (~82.9% screen-to-body ratio)\\\",\\\"Resolution\\\":\\\"1080 x 2400 pixels, 20:9 ratio (~386 ppi density)\\\",\\\"OS\\\":\\\"Android 10, upgradable to Android 11, Android One\\\",\\\"Chipset\\\":\\\"Qualcomm SM7250 Snapdragon 765G 5G (7 nm)\\\",\\\"CPU\\\":\\\"Octa-core (1x2.4 GHz Kryo 475 Prime & 1x2.2 GHz Kryo 475 Gold & 6x1.8 GHz Kryo 475 Silver)\\\",\\\"GPU\\\":\\\"Adreno 620\\\",\\\"Card slot\\\":\\\"microSDXC (uses shared SIM slot)\\\",\\\"Internal\\\":\\\"64GB 6GB RAM, 64GB 8GB RAM, 128GB 8GB RAM\\\",\\\"Quad\\\":\\\"64 MP, f\\\\/1.9, (wide), 1\\\\/1.72\\\\\\\", 0.8\\\\u00b5m, PDAF\\\\r\\\\n 12 MP, f\\\\/2.2, 120\\\\u02da (ultrawide), 1\\\\/2.43\\\\\\\", 1.4\\\\u00b5m, AF\\\\r\\\\n 2 MP, (macro)\\\\r\\\\n 2 MP, (depth)\\\",\\\"Features\\\":\\\"Zeiss optics, HDR\\\",\\\"Video\\\":\\\"1080p#30fps\\\",\\\"Single\\\":\\\"24 MP, f\\\\/2.0, (wide), 1\\\\/2.8\\\\\\\", 0.9\\\\u00b5m\\\",\\\"Loudspeaker \\\":\\\"Yes\\\",\\\"3.5mm jack \\\":\\\"Yes\\\",\\\"WLAN\\\":\\\"Wi-Fi 802.11 a\\\\/b\\\\/g\\\\/n\\\\/ac, dual-band, Wi-Fi Direct, hotspot\\\",\\\"Bluetooth\\\":\\\"5.0, A2DP, EDR, LE\\\",\\\"GPS\\\":\\\"Yes, with A-GPS, GLONASS, BDS\\\",\\\"NFC\\\":\\\"Yes\\\",\\\"Radio\\\":\\\"FM radio\\\",\\\"USB\\\":\\\"USB Type-C 2.0, USB On-The-Go\\\",\\\"Sensors\\\":\\\"Fingerprint (side-mounted), accelerometer, gyro, proximity, compass\\\",\\\"Charging\\\":\\\"Fast charging 18W\\\",\\\"Colors\\\":\\\"Polar Night\\\",\\\"Models\\\":\\\"TA-1243, TA-1251\\\",\\\"SAR\\\":\\\"0.96 W\\\\/kg (head) 1.41 W\\\\/kg (body) \\\",\\\"SAR EU\\\":\\\"0.96 W\\\\/kg (head) 1.41 W\\\\/kg (body) \\\",\\\"Price\\\":\\\"$ 433.90 \\\\/ € 574.35 \\\\/ £ 349.00\\\"}",
"DeletedAt":null,
"CreatedAt":"2021-09-10T00:45:32",
}
PRICE property is inside Specification
\"Price\":\"$ 433.90 \\/ € 574.35 \\/ £ 349.00\"

The property Specifications has been corrupted. Someone has tried to escape the character for inches ", found in the Quad and Single properties, but they have escaped every single " in the JSON string! This needs to be fixed at the source.
Here's the Quad property found in Specifications
\\\"Quad\\\":\\\"64 MP, f\\\\/1.9, (wide), 1\\\\/1.72\\\\\\\", 0.8\\\\u00b5m, PDAF\\\\r\\\\n 12 MP, f\\\\/2.2, 120\\\\u02da (ultrawide), 1\\\\/2.43\\\\\\\", 1.4\\\\u00b5m, AF\\\\r\\\\n 2 MP, (macro)\\\\r\\\\n 2 MP, (depth)\\\"
and here's what it should be:
"Quad":"64 MP, f/1.9, (wide), 1/1.72\\\", 0.8u00b5m, PDAFrn 12 MP, f/2.2, 120u02da (ultrawide), 1/2.43\\\", 1.4u00b5m, AFrn 2 MP, (macro)rn 2 MP, (depth)"
Also, the Specifications value should not be surrounded in quotations.
"Specifications": {"Technology": "GSM / HSPA / LTE / 5G", ... },
I get the feeling someone is manually creating JSON strings instead of using a library. This a great example why you should never do that.

You just need to parse the JSON:
try {
const myObject: any = JSON.parse(YOUR_JSON_STRING_HERE);
const price = myObject.Specifications.Price;
console.log("Price:", price);
} catch (error) {
console.log(error);
}
if it's an array:
const price = myObjects[0].Specifications.Price;

Related

Creating a percentage in SQL Server using a counter

Suppose you have this data in the table 'Student':
ID COUNTY
------------------
1 Millwood
2 Jackson
3 Kendall
4 Millwood
5 Worth
6 Belton
7 Belton
8 Worth
9 Jackson
10 Millwood
11 Kendall
12 Worth
13 Belton
14 Belton
15 Maxwell
16 Maxwell
17 Maxwell
18 Maxwell
19 Maxwell
20 Johnson
Millwood has 3 people, Jackson has 2, Kendall has 2, Worth has 3, Belton has 4, Maxwell has 5, and Johnson has 1. Let's say I want the top 5 counties with the greatest population. That would be:
Maxwell 5
Belton 4
Worth 3
Millwood 3
Jackson 2
Kendall 2
Now, let's say I want an output with a percentage of the total population. By this data, there are 20 total people among those six counties (an entire state). Maxwell has 25% of the population, Belton has 20%, Worth and Millwood have 15% each, and Jackson and Kendall have 10% each. How would I get that output in SQL Server?
Maxwell 25%
Belton 20%
Worth 15%
Millwood 15%
Jackson 10%
Kendall 10%
I have to link two tables to get a county name. This code below has not worked:
SELECT
countycodes.countyname,
SUM(student.mstid) AS tot_stdts_by_county,
ROUND(SUM(student.mstid) / SUM(SUM(student.mstid)) OVER (), 2) * 100 AS pct
FROM
Student
INNER JOIN
CountyCodes ON Student.MCOUNTY = CountyCodes.CountyCode
AND Student.MSTATE = CountyCodes.StateCode
WHERE
(Student.MSTLEV IN (1, 2, 3, 4))
GROUP BY
Student.MRPTYR, Student.MRPTTRM, CountyCodes.CountyCode,
Student.MCOUNTY, Student.MENTST, CountyCodes.CountyName,
Student.MSTATE, CountyCodes.StateCode
HAVING
(Student.MRPTTRM = 'sa')
AND (Student.MRPTYR = 2007)
AND (Student.MENTST = 'ZY')
ORDER BY
tot_stdts_by_county DESC

Dealing with large grib files using xarray and dask

I'm reading some (apparently) large grib files using xarray. I say 'apparently' because they're ~100MB each, which doesn't seem too big to me. However, running
import xarray as xr
ds = xr.open_dataset("gribfile.grib", engine="cfgrib")
takes a good 5-10 minutes. Worse, reading one of these takes up almost 4GB RAM - something that surprises me given the lazy-loading that xarray is supposed to do. Not least that this is 40-odd times the size of the original file!
This reading time and RAM usage seems excessive and isn't scalable to the 24 files I have to read.
I've tried using dask and xr.open_mfdataset, but this doesn't seem to help when the individual files are so large. Any suggestions?
Addendum:
dataset looks like this once opened:
<xarray.Dataset>
Dimensions: (latitude: 10, longitude: 10, number: 50, step: 53, time: 45)
Coordinates:
* number (number) int64 1 2 3 4 5 6 7 8 9 ... 42 43 44 45 46 47 48 49 50
* time (time) datetime64[ns] 2011-01-02 2011-01-04 ... 2011-03-31
* step (step) timedelta64[ns] 0 days 00:00:00 ... 7 days 00:00:00
surface int64 0
* latitude (latitude) float64 56.0 55.0 54.0 53.0 ... 50.0 49.0 48.0 47.0
* longitude (longitude) float64 6.0 7.0 8.0 9.0 10.0 ... 12.0 13.0 14.0 15.0
valid_time (time, step) datetime64[ns] 2011-01-02 ... 2011-04-07
Data variables:
u100 (number, time, step, latitude, longitude) float32 6.389208 ... 1.9880934
v100 (number, time, step, latitude, longitude) float32 -13.548858 ... -3.5112982
Attributes:
GRIB_edition: 1
GRIB_centre: ecmf
GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts
GRIB_subCentre: 0
history: GRIB to CDM+CF via cfgrib-0.9.4.2/ecCodes-2.9.2 ...
I've temporarily got around the issue by reading in the grib files, one-by-one, and writing them to disk as netcdf. xarray then handles the netcdf files as expected. Obviously it would be nice to not have to do this because it takes ages - I've only done this for 4 so far.

Understanding input and labels in word2vec (TensorFlow)

I am trying to properly understand the batch_input and batch_labels from the tensorflow "Vector Representations of Words" tutorial.
For instance, my data
1 1 1 1 1 1 1 1 5 251 371 371 1685 ...
... starts with
skip_window = 2 # How many words to consider left and right.
num_skips = 1 # How many times to reuse an input to generate a label.
Then the generated input array is:
bach_input = 1 1 1 1 1 1 5 251 371 ....
This makes sense, starts from after 2 (= window size) and then continuous. The labels:
batch_labels = 1 1 1 1 1 1 251 1 1685 371 589 ...
I don't understand these labels very well. There are supposed to be 4 labels for each input right (window size 2, on each side). But the batch_label variable is the same length.
From the tensorflow tutorial:
The skip-gram model takes two inputs. One is a batch full of integers
representing the source context words, the other is for the target
words.
As per the tutorial, I have declared the two variables as:
batch = np.ndarray(shape=(batch_size), dtype=np.int32)
labels = np.ndarray(shape=(batch_size, 1), dtype=np.int32)
How should I interpret the batch_labels?
There are supposed to be 4 labels for each input right (window size 2, on each side). But the batch_label variable is the same length.
The key setting is num_skips = 1. This value defines the number of (input, label) tuples each word generates. See the examples with different num_skips below (my data sequence seems to be different from yours, sorry about that).
Example #1 - num_skips=4
batch, labels = generate_batch(batch_size=8, num_skips=4, skip_window=2)
It generates 4 labels for each word, i.e. uses the whole context; since batch_size=8 only 2 words are processed in this batch (12 and 6), the rest will go into the next batch:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [12 12 12 12 6 6 6 6]
labels = [[6 3084 5239 195 195 3084 12 2]]
Example #2 - num_skips=2
batch, labels = generate_batch(batch_size=8, num_skips=2, skip_window=2)
Here you would expect each word appear twice in the batch sequence; the 2 labels are randomly sampled from 4 possible words:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [ 12 12 6 6 195 195 2 2]
labels = [[ 195 3084 12 195 3137 12 46 195]]
Example #3 - num_skips=1
batch, labels = generate_batch(batch_size=8, num_skips=1, skip_window=2)
Finally, this setting, same as yours, produces exactly one label per each word; each label is drawn randomly from the 4-word context:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [ 12 6 195 2 3137 46 59 156]
labels = [[ 6 12 12 195 59 156 46 46]]
How should I interpret the batch_labels?
Each label is the center word to be predicted from the context. But the generated data may take not all (context, center) tuples, depending on the settings of the generator.
Also note that the train_labels tensor is 1-dimensional. Skip-Gram trains the model to predict any context word from the given center word, not all 4 context words at once. This explains why all training pairs (12, 6), (12, 3084), (12, 5239) and (12, 195) are valid.

Closeness score to a target array of numbers

It's hard to know what terms to search for on stackoverflow for this problem. Say you have a target array of numbers like [100, 250, 400, 60]
I want to be able to score the closeness other arrays have to this target based on a threshold / error bars of say 10. So for example, the array:
[90, 240, 390, 50] would get a high score (or positive match result) because of the error bars.
The order matters, so
[60, 400, 250, 100] would get zero score (or negative match result)
The arrays can be different sizes so
[33, 77, 300, 110, 260, 410, 60, 99, 23] would get good score or positive match result.
A good way to think about the problem is to imagine these numbers are frequencies of musical notes like C,G,E,F and I'm trying to match a sequence of notes against a target.
Searching stackoverflow I'm not sure is this post will work, but it's close:
Compare difference between multiple numbers
Update 17th Jan 2015:
I failed to mention a scenario that might affect current answers. If the array has noise between those target numbers, I still want to find a positive match. For example [33, 77, 300, 110, 260, 300, 410, 40, 60, 99, 23].
I believe what you're looking for is sequence similarity.
You can read about them on this wikipedia page. Your case seems fit to local alignment category. There's some algorithm you can choose :
Needleman–Wunsch algorithm
Levenshtein distance
However, since these algorithms compare strings, you have to design your own scoring rule when inserting, deleting or comparing numbers.
Sounds like what you're looking for is the RMS error, where RMS is the square Root of the Mean Squared error. Let me illustrate by example. Assume the target array is [100, 250, 400, 60] and the array to be scored is [104, 240, 410, 55]
First compute the difference values, i.e. the errors
100 250 400 60
-104 -240 -410 -55
---- ---- ---- ---
-4 10 -10 5
Then square the errors to get 16 100 100 25. Compute the mean of the squared errors
(16 + 100 + 100 + 25) / 4 = 60.25
And finally, take the square root sqrt(60.25) = 7.76
When the arrays are different sizes, you can speed things up by only computing the RMS error if the first value is within a certain threshold, say +- 30. Using the example [33, 77, 300, 110, 260, 410, 60, 99, 23], there would only be two alignments to check, because with the other alignments the first number is more than 30 away from 100
33 77 300 110 260 410 60 99 23
100 250 400 60 --> RMS score = 178
100 250 400 60 --> RMS score = 8.7
Low score wins!

separate chaining vs linear probing

a set of objects with keys: 12, 44, 13, 88, 23, 94, 11, 39, 20, 16, 5
Write the hash table where M=N=11 and collisions are handled using separate chaining.
h(x) = | 2x + 5 | mod M
So I did it with linear probing and got
11 39 20 5 16 44 88 12 23 13 94
which I am pretty sure is right, but how do you do it with separate chaining? I realize separate chaining uses linked lists, but how would the hash table look like?

Resources