Regex to pick values from specific json key - json

My json string:
{
"computed_at": "2022-11-29T08:21:47.904741+00:00",
"data":
{
"2022-11-07":
{
"steps":
[
{
"count": 1853,
"avg_time": null,
"avg_time_from_start": null,
"session_event": "start",
"goal": "start",
"step_label": "Came to Corporate",
"overall_conv_ratio": 1,
"step_conv_ratio": 1
},
{
"count": 741,
"avg_time": 25451,
"avg_time_from_start": 25451,
"time_buckets_from_prev":
{
"lower": 0,
"higher": 0,
"buckets":
[
720,
2,
3,
4,
3,
3,
3,
1,
0,
2,
0,
0,
0,
0,
0
]
},
"time_buckets_from_start":
{
"lower": 0,
"higher": 0,
"buckets":
[
720,
2,
3,
4,
3,
3,
3,
1,
0,
2,
0,
0,
0,
0,
0
]
},
"event": "Viewed Collection Page",
"goal": "Viewed Collection Page",
"step_label": "Viewed Collection Page",
"overall_conv_ratio": 0.39989206691851054,
"step_conv_ratio": 0.39989206691851054
},
{
"count": 174,
"avg_time": 53538,
"avg_time_from_start": 110700,
"time_buckets_from_prev":
{
"lower": 0,
"higher": 0,
"buckets":
[
163,
2,
1,
3,
0,
2,
1,
2,
0,
0,
0,
0,
0,
0,
0
]
},
"time_buckets_from_start":
{
"lower": 0,
"higher": 0,
"buckets":
[
151,
4,
2,
5,
1,
4,
3,
3,
0,
1,
0,
0,
0,
0,
0
]
},
"event": "Product Viewed",
"goal": "Product Viewed",
"step_label": "Product Viewed",
"overall_conv_ratio": 0.09390178089584458,
"step_conv_ratio": 0.23481781376518218
}
],
"analysis":
{
"completion": 174,
"starting_amount": 1853,
"steps": 3,
"worst": 2
}
}
},
"meta":
{
"dates":
[
"2022-11-07"
],
"property_values":
[],
"min_sampling_factor": 1,
"group_by_metadata":
[]
},
"min_sampling_factor": 1
}
I want to extract the values for these particular keys (each of which has 3 instances in the above json string):
count
step_conv_ratio
What is the correct regex so that I will get 1853, 741, 174 for count; and 1, 0.39989206691851054, 0.23481781376518218 for step_conv_ratio?
Unmarshalling this to a Go struct is not ideal, since it has dynamic json field name, so I cannot use a static struct with static json tag to unmarshal. Hence I'm thinking of using regex.

Consider the comments about unmarshalling. Else the regex you are looking for is:
`"count": ([0-9]+)|"step_conv_ratio": ([0-9.]+)`
then the number will be in the second or the third element of the slice that contains the matches (e.g. after FindAllStringSubmatch()) depending on it's a count or a step_conv_ratio.

Related

Mapping an array in inside another array and display into a table in more than one entry

I have data that include the result of student each in an array but also with different subject basing on subject that they undertake. I want to map through them to be able to insert it into a table with respect to the subject and they mark which has to be matching.
This is my API result
{
"status": 200,
"message": "Successfully retrieve",
"data": [
{
"studentNumber": "22001",
"gender": "M",
"result": [
{
"moduleCode": "CHE1163",
"continousAssesment": 42,
"Exam": 6,
"workMaxContinousAssement": 55,
"actualContinousAssesment": 38.18181818181819,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 44.18181818181819,
"studentNumber": "22001",
"moduleCredit": 10
},
{
"moduleCode": "MEE1162",
"continousAssesment": 10,
"Exam": 40,
"workMaxContinousAssement": 45,
"actualContinousAssesment": 11.11111111111111,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 51.111111111111114,
"studentNumber": "22001",
"moduleCredit": 15
}
]
},
{
"studentNumber": "1111",
"gender": "M",
"result": [
{
"moduleCode": "CHE1163",
"continousAssesment": 0,
"Exam": 23,
"workMaxContinousAssement": 0,
"actualContinousAssesment": 0,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 23,
"studentNumber": "1111",
"moduleCredit": 10
},
{
"moduleCode": "MEE1162",
"continousAssesment": 12,
"Exam": 20,
"workMaxContinousAssement": 45,
"actualContinousAssesment": 13.333333333333334,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 33.333333333333336,
"studentNumber": "1111",
"moduleCredit": 15
}
]
},
{
"studentNumber": "667",
"gender": "F",
"result": [
{
"moduleCode": "CHE1163",
"continousAssesment": 0,
"Exam": 3,
"workMaxContinousAssement": 0,
"actualContinousAssesment": 0,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 3,
"studentNumber": "667",
"moduleCredit": 10
},
{
"moduleCode": "MEE1162",
"continousAssesment": 17,
"Exam": 40,
"workMaxContinousAssement": 45,
"actualContinousAssesment": 18.88888888888889,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 58.888888888888886,
"studentNumber": "667",
"moduleCredit": 15
}
]
}
]
}
the result I should be expecting is something like this
I have tried to map from to but it showing nothing when I replace the value as variable
this is the part of the table code
overallResult.map((item) => {
return (
<>
<tr>
<td></td>
<td>{item.studentNumber}</td>
<td className="font-medium">{item.gender}</td>
<td>44.18181818181819</td>
<td>51.111111111111114</td>
<td>45</td>
</tr>
</>
)
}
This should be mapping and then insert the actual value into the for the map which is corresponding to the student as seen in table picture
I updated your sandbox link.
Instead of hard coded data, now your table is populated with the data from overallResult object.
It is not perfect, but at least you can see how to loop through a data and display on the screen.
https://codesandbox.io/s/angry-bardeen-brkzf5?file=/src/App.js

How to turn a column into a row via JMESPath in Google sheets?

Good afternoon!
I am working on api integration with google sheets using the API to Sheets extension https://workspace.google.com/marketplace/app/api_to_sheets/245778206812.
Can't solve JMESPath Query problem, need your help
Initial code
{
"result": {
"postings": [
{
"posting_number": "49382959-0024-1",
"order_id": 633398070,
"order_number": "49382959-0024",
"status": "delivered",
"delivery_method": {
"id": 22038534499000,
"name": "Ozon Логистика самостоятельно, Москва",
"warehouse_id": 22038534499000,
"warehouse": "Московская обл., г. Видное, ул. Завидная 19",
"tpl_provider_id": 24,
"tpl_provider": "Ozon Логистика"
},
"tracking_number": "",
"tpl_integration_type": "ozon",
"in_process_at": "2022-04-08T04:56:02Z",
"shipment_date": "2022-04-09T10:00:00Z",
"delivering_date": "2022-04-09T07:14:26Z",
"cancellation": {
"cancel_reason_id": 0,
"cancel_reason": "",
"cancellation_type": "",
"cancelled_after_ship": false,
"affect_cancellation_rating": false,
"cancellation_initiator": ""
},
"customer": null,
"products": [
{
"price": "7970.0000",
"offer_id": "R22301Е",
"name": "Смеситель для ванны и душа Pursh R22301Е черный, нержавеющая сталь",
"sku": 300454648,
"quantity": 1,
"mandatory_mark": [],
"currency_code": ""
}
],
"addressee": null,
"barcodes": null,
"analytics_data": {
"region": "Ульяновская Область",
"city": "Ульяновск",
"delivery_type": "PVZ",
"is_premium": false,
"payment_type_group_name": "Карты оплаты",
"warehouse_id": 22038534499000,
"warehouse": "Московская обл., г. Видное, ул. Завидная 19",
"tpl_provider_id": 24,
"tpl_provider": "Ozon Логистика",
"delivery_date_begin": "2022-04-16T13:00:00Z",
"delivery_date_end": "2022-04-16T17:00:00Z",
"is_legal": false
},
"financial_data": {
"products": [
{
"commission_amount": 637.6,
"commission_percent": 8,
"payout": 7332.4,
"product_id": 300454648,
"old_price": 15900,
"price": 7970,
"total_discount_value": 7930,
"total_discount_percent": 49.87,
"actions": [
"Системная виртуальная скидка селлера",
"Маркетплейс промо №4"
],
"picking": null,
"quantity": 1,
"client_price": "",
"item_services": {
"marketplace_service_item_fulfillment": 0,
"marketplace_service_item_pickup": 0,
"marketplace_service_item_dropoff_pvz": 0,
"marketplace_service_item_dropoff_sc": 0,
"marketplace_service_item_dropoff_ff": 0,
"marketplace_service_item_direct_flow_trans": -54,
"marketplace_service_item_return_flow_trans": 0,
"marketplace_service_item_deliv_to_customer": 0,
"marketplace_service_item_return_not_deliv_to_customer": 0,
"marketplace_service_item_return_part_goods_customer": 0,
"marketplace_service_item_return_after_deliv_to_customer": 0
}
}
],
"posting_services": {
"marketplace_service_item_fulfillment": 0,
"marketplace_service_item_pickup": 0,
"marketplace_service_item_dropoff_pvz": -45,
"marketplace_service_item_dropoff_sc": 0,
"marketplace_service_item_dropoff_ff": 0,
"marketplace_service_item_direct_flow_trans": 0,
"marketplace_service_item_return_flow_trans": 0,
"marketplace_service_item_deliv_to_customer": -350,
"marketplace_service_item_return_not_deliv_to_customer": 0,
"marketplace_service_item_return_part_goods_customer": 0,
"marketplace_service_item_return_after_deliv_to_customer": 0
}
},
"is_express": false,
"requirements": {
"products_requiring_gtd": [],
"products_requiring_country": [],
"products_requiring_mandatory_mark": []
}
},
{
"posting_number": "07227925-0084-1",
"order_id": 633125045,
"order_number": "07227925-0084",
"status": "delivered",
"delivery_method": {
"id": 22038534499000,
"name": "Ozon Логистика самостоятельно, Москва",
"warehouse_id": 22038534499000,
"warehouse": "Московская обл., г. Видное, ул. Завидная 19",
"tpl_provider_id": 24,
"tpl_provider": "Ozon Логистика"
},
"tracking_number": "",
"tpl_integration_type": "ozon",
"in_process_at": "2022-04-07T21:20:13Z",
"shipment_date": "2022-04-09T10:00:00Z",
"delivering_date": "2022-04-09T07:14:34Z",
"cancellation": {
"cancel_reason_id": 0,
"cancel_reason": "",
"cancellation_type": "",
"cancelled_after_ship": false,
"affect_cancellation_rating": false,
"cancellation_initiator": ""
},
"customer": null,
"products": [
{
"price": "2763.0000",
"offer_id": "LА1061W",
"name": "Белый однорычажный смеситель для раковины (умывальника) из нержавеющей стали, Lemen",
"sku": 519731707,
"quantity": 1,
"mandatory_mark": [],
"currency_code": ""
},
{
"price": "2677.0000",
"offer_id": "R933O3W",
"name": "Высокий однорычажный смеситель для кухни, Pursh, белый, нержавеющая сталь",
"sku": 545484671,
"quantity": 1,
"mandatory_mark": [],
"currency_code": ""
}
],
"addressee": null,
"barcodes": null,
"analytics_data": {
"region": "Московская Область",
"city": "Красногорск",
"delivery_type": "Courier",
"is_premium": false,
"payment_type_group_name": "Карты оплаты",
"warehouse_id": 22038534499000,
"warehouse": "Московская обл., г. Видное, ул. Завидная 19",
"tpl_provider_id": 24,
"tpl_provider": "Ozon Логистика",
"delivery_date_begin": "2022-04-11T09:00:00Z",
"delivery_date_end": "2022-04-11T19:00:00Z",
"is_legal": false
},
"financial_data": {
"products": [
{
"commission_amount": 221.04,
"commission_percent": 8,
"payout": 2541.96,
"product_id": 519731707,
"old_price": 9900,
"price": 2763,
"total_discount_value": 7137,
"total_discount_percent": 72.09,
"actions": [
"Системная виртуальная скидка селлера",
"Маркетплейс промо №4"
],
"picking": null,
"quantity": 1,
"client_price": "",
"item_services": {
"marketplace_service_item_fulfillment": 0,
"marketplace_service_item_pickup": 0,
"marketplace_service_item_dropoff_pvz": 0,
"marketplace_service_item_dropoff_sc": 0,
"marketplace_service_item_dropoff_ff": 0,
"marketplace_service_item_direct_flow_trans": -37.8,
"marketplace_service_item_return_flow_trans": 0,
"marketplace_service_item_deliv_to_customer": 0,
"marketplace_service_item_return_not_deliv_to_customer": 0,
"marketplace_service_item_return_part_goods_customer": 0,
"marketplace_service_item_return_after_deliv_to_customer": 0
}
},
{
"commission_amount": 214.16,
"commission_percent": 8,
"payout": 2462.84,
"product_id": 545484671,
"old_price": 6750,
"price": 2677,
"total_discount_value": 4073,
"total_discount_percent": 60.34,
"actions": [
"Системная виртуальная скидка селлера",
"Маркетплейс промо №4"
],
"picking": null,
"quantity": 1,
"client_price": "",
"item_services": {
"marketplace_service_item_fulfillment": 0,
"marketplace_service_item_pickup": 0,
"marketplace_service_item_dropoff_pvz": 0,
"marketplace_service_item_dropoff_sc": 0,
"marketplace_service_item_dropoff_ff": 0,
"marketplace_service_item_direct_flow_trans": -43.2,
"marketplace_service_item_return_flow_trans": 0,
"marketplace_service_item_deliv_to_customer": 0,
"marketplace_service_item_return_not_deliv_to_customer": 0,
"marketplace_service_item_return_part_goods_customer": 0,
"marketplace_service_item_return_after_deliv_to_customer": 0
}
}
],
"posting_services": {
"marketplace_service_item_fulfillment": 0,
"marketplace_service_item_pickup": 0,
"marketplace_service_item_dropoff_pvz": -45,
"marketplace_service_item_dropoff_sc": 0,
"marketplace_service_item_dropoff_ff": 0,
"marketplace_service_item_direct_flow_trans": 0,
"marketplace_service_item_return_flow_trans": 0,
"marketplace_service_item_deliv_to_customer": -272,
"marketplace_service_item_return_not_deliv_to_customer": 0,
"marketplace_service_item_return_part_goods_customer": 0,
"marketplace_service_item_return_after_deliv_to_customer": 0
}
},
"is_express": false,
"requirements": {
"products_requiring_gtd": [],
"products_requiring_country": [],
"products_requiring_mandatory_mark": []
}
}
],
"has_next": false
}
}
JMESPath
result.postings[].{posting_number:posting_number, products:products[].offer_id}
Gives me data
But I need the data to move
And it was like in the last picture
In other words how to turn a column into a row via JMESPath
Please help!
Try this:
function colintorow(col=1) {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const column = [sh.getRange(1,col,sh.getLastRow()).getValues().flat()];
Logger.log(JSON.stringify(column));
}

Is it possible to output a JSON object row by row in Angular?

I have a field "properties" in my table(data type: text). In this field there is a JSON object. Now I want to output it in Angular in a reasonable way as I have shown below. Currently only the JSON format is displayed.
There must be a way to display the attributes of a JSON format row by row, right?
"properties": "{'width': 4, 'height': 14, 'count': 10, 'qm': 63, 'loc_x': 0, 'loc_y': 0, 'loc_x': 1}",
width: 4
height: 14
count: 10
qm: 63
loc_x: 0
loc_y: 0
loc_x: 1
UDATE 1:
<div *ngFor="let property of properties | keyvalue">
{{property.key}}: {{property.value}}
</div>
UPDATE 2:
This is my JSON from the Backend
{
"id": 1,
"formId": 1,
"properties": "{'width': 4, 'height': 14, 'count': 10, 'qm': 63, 'loc_x': 0, 'loc_y': 0, 'loc_x': 1}",
"components": [
{
"id": 1,
"shapeId": 1,
"properties": "{'width': 4, 'height': 14, 'count': 10, 'qm': 63, 'loc_x': 0, 'loc_y': 0, 'loc_x': 1}",
"shapeComponentMaterials": [
{
"id": 1,
"componentId": 1,
},
{
"id": 15,
"componentId": 1,
}
]
},
{
"id": 11,
"shapeId": 1,
"properties": "{'width': 4, 'height': 14, 'count': 10, 'qm': 63, 'loc_x': 0, 'loc_y': 0, 'loc_x': 1}",
"shapeComponentMaterials": [
{
"id": 11,
"componentId": 11,
}
]
}
]
}
This is my shape.ts
export interface Shape {
id?: number;
formId: number;
properties?: string;
components?: Array<Component>;
}
You can make use of Angular's KeyValuePipe to display the data in the desired format:
<div *ngFor="let property of properties | keyvalue">
{{property.key}}:{{property.value}}
</div>
UPDATE - Extending answer as per request
As you have updated your question with the exact API response, here's the method to access the properties property to display it in HTML as required:
accessingProperties = JSON.parse(this.valueFromAPI.properties.replace(/'/g, '"'));
Here's a Stackblitz with this example in live.

How to do arithmetic substraction of same identical key values between 2 json files and print it in output json file

I've 2 json files of with identical key fields on both the file. I would like to get your assistance to do arithmetic subtraction of values of same key field between 2 files and present in 3rd output file [ delta of two json files to output json file].
Also 1st and 2nd json files has n of array of indicies so we need to do this in loop to map the difference values in 3rd json output file.
Example:
1st Json file: file1.json
[
{
"name": "Rock",
"pri": 21,
"size": 502173,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_current": 0,
"merges_total": 0,
"refresh_total": 0
},
{
"name": "Rock:config",
"pri": 21,
"size": 512173,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_curr": 0,
"merges_tot": 0,
"refresh_tot": 0
}
]
2nd Json file: file1.json:
[
{
"name": "Rock",
"pri": 22,
"size": 602173,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_current": 0,
"merges_total": 0,
"refresh_total": 0
},
{
"name": "Rock:config",
"pri": 31,
"size": 602173,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_curr": 0,
"merges_tot": 0,
"refresh_tot": 0
}
]
Output json file: file3.json should look like below.
[
{
"name": "Rock",
"pri": 1,
"size": 100000,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_current": 0,
"merges_total": 0,
"refresh_total": 0
},
{
"name": "Rock:config",
"pri": 10,
"size": 90000,
"gets": 0,
"searches": 40,
"search_time_ms": 25,
"fetches": 2,
"cache_mem_size": 0,
"cache_size": 0,
"cache_total": 2,
"hits": 0,
"misses": 2,
"index_total": 0,
"index_curr": 0,
"merges_tot": 0,
"refresh_tot": 0
}
]
please help me with the logic
This can be done as simple as u follow the following steps
Read the json object from 2 json files and save them as list of dictionaries
Loop through the list of dicts and perform your required manipulations
Save calculated output dicts to a new list
Write the resultant list to a new json file
the code is as follows,
import json
with open('file1.json') as f:
data1 = json.load(f)
with open('file2.json') as f:
data2 = json.load(f)
data = []
for i,j in zip(data1, data2):
x = {}
for m,n in zip(i.items(), j.items()):
# if m[0] != "name":
if m[0] == "pri" or m[0] == "size":
x[m[0]] = int(j[m[0]]) - int(i[m[0]])
else:
x[m[0]] = i[m[0]]
data.append(x)
with open('file3.json', 'w') as o:
json.dump(data, o)

Need to import JSON data to Simple html file

Is there a way to import some data from json file into html file.
In need to import some of these data to a html file,
for example "Descrizione" and "Listino1" in a simple div.
Following is my json file sample:
[
{
"OP": "save",
"DB_NAME": "plu",
"ID": "005045",
"Barcode": "",
"CodVeloce": 0,
"CodPv": 0,
"FlagCondiviso": 1,
"CodTaccuino": 0,
"Descrizione": "BICC. VINO BIANCO",
"Famiglia": "006",
"ScortaMinima": 0,
"Posizione": 181,
"Reparto": 2,
"CodImmagini": 0,
"Listino1":4.00,
"Listino2":4.00,
"Listino3":0.00,
"Listino4":0.00,
"Stampante": 0,
"Flag": 1,
"Offerta": 0,
"Colore": 19,
"UnitMis": "",
"CodFornitore": 0,
"CodArtFornitore": "",
"BarcodeConfezione": "",
"PezziXConfezione": 0,
"ForzaLavoro": 0,
"PesoUnitario":0.00,
"NumConfezioni": 0,
"DescrizAggiuntiva": "",
"OffertaAggiuntiva": 0,
"PrezzoConfezione":0.00,
"ColoreTac": 0,
"Scala": 0,
"DtVar": "2020/02/03"
},
Any help.
Thanks in advance