There is a JSON string, where we have engagedPartners array:
{
"sbData": [],
"listing": [
{
"db": {
"name": "abcd"
}
}
],
"audienceStats": {
"audienceStatus": 0
},
"userStats": {
"audienceName": "testWithNoCategory-1",
"audienceStatus": 1,
"engagedpartners": [
{
"name": "partner samWebsite 19999",
"viewsCount": 165,
"areaPage": 0,
"areaPageDescription": "xyz"
},
{
"name": "partner samWebsite 19999",
"viewsCount": 336,
"areaPage": 1,
"areaPageDescription": "Promotions"
},
{
"name": "partner samWebsite 19999",
"viewsCount": 1,
"areaPage": 2,
"areaPageDescription": "Vehicle Details"
},
{
"name": "partner samWebsite 19999",
"viewsCount": 1,
"areaPage": 3,
"areaPageDescription": "Search Results"
},
{
"name": "partner samWebsite 19999",
"viewsCount": 1,
"areaPage": 4,
"areaPageDescription": "About Us"
}
]
}
}
Inside it we need to update the key "areaPage" to "productPage" through this inner array using postgres, and the expected output should be shown below:
{
"sbData": [],
"listing": [
{
"db": {
"name": "abcd"
}
}
],
"audienceStats": {
"audienceStatus": 0
},
"userStats": {
"audienceName": "testWithNoCategory-1",
"audienceStatus": 1,
"engagedpartners": [
{
"name": "partner samWebsite 19999",
"viewsCount": 165,
"productPage": 0,
"productPageDescription": "xyz"
},
{
"name": "partner samWebsite 19999",
"viewsCount": 336,
"productPage": 1,
"productPageDescription": "Promotions"
},
{
"name": "partner samWebsite 19999",
"viewsCount": 1,
"productPage": 2,
"productPageDescription": "Vehicle Details"
},
{
"name": "partner samWebsite 19999",
"viewsCount": 1,
"productPage": 3,
"productPageDescription": "Search Results"
},
{
"name": "partner samWebsite 19999",
"viewsCount": 1,
"productPage": 4,
"productPageDescription": "About Us"
}
]
}
}
I tried various options to reach out to this inner array and then update it but couldn't yet work.
1st way.
if your JSON data is not stored on rows in the table or is not large in count then you can cast your JSON to text and after then replace text and after then you can again cast to JSON. This is one of the ways. But for many JSON data this way is not recommended.
2nd way.
If you have many data, please write me I will write for you query for replacing this using JSON and JSON ARRAY functions on PostgreSQL.
Example about 1st way:
select replace(replace(jsondata::text, 'areaPage', 'productPage'), 'areaPageDescription', 'productPageDescription')::jsonb from your_table
Related
i have a question:
Is it possible read a json file and convert to dataframe dynamically?
My example is the next code:
Having this json file, i need 3 table dataframes:
{
"date_time": "01-03-2022, 15:18:32",
"regions": {
"Home Region": "Madrid",
"Primary Region": "Barcelona",
"Secondary Region": "Rio"
},
"customers": [
{
"name": "campo santo",
"address": "rua trebal 1",
"phone": 987456321,
"parking": true
},
{
"name": "santo da silva",
"address": "rua sama 6",
"phone": 654321987,
"parking": false
},
{
"name": "roger campos",
"address": "av casal 10",
"phone": 684426654,
"parking": true
}
],
"office": [
{
"location": "madrid",
"co_working_spaces": 25,
"kitchen": false,
"food_track": 2,
"internal_staff": [
{
"id": 123,
"name": "pablo"
},
{
"id": 874,
"name": "saul"
},
{
"id": 741,
"name": "maria"
}
]
},
{
"location": "rio",
"co_working_spaces": 55,
"kitchen": true,
"food_track": 4,
"internal_staff": [
{
"id": 784,
"name": "raquel"
},
{
"id": 874,
"name": "pedro"
},
{
"id": 145,
"name": "maria"
},
{
"id": 365,
"name": "rocio"
}
]
},
{
"location": "barcelona",
"co_working_spaces": 5,
"kitchen": false,
"food_track": 1,
"internal_staff": [
]
},
{
"location": "la",
"co_working_spaces": 5,
"kitchen": true,
"food_track": 4,
"internal_staff": [
{
"id": 852,
"name": "maria"
},
{
"id": 748,
"name": "sara"
}
]
}
]
}
this is my python code:
import pandas as pd
# from pandas.io.json import json_normalize
import json
with open('offices.json') as f:
dt = json.load(f)
# df = pd.json_normalize(dt)
df1 = pd.json_normalize(dt, 'customers', 'date_time')[['name', 'address', 'phone', 'parking', 'date_time']]
print(df1)
df2 = pd.json_normalize(dt, 'office', 'date_time')[['location', 'co_working_spaces', 'kitchen', 'food_track']]
print(df2)
df3 = pd.json_normalize(dt['office'], 'internal_staff', 'location')
print(df3)
With this code, i got my 3 table dataframes. But i have to know the json structure to create the dataframes.
So is it possible to do it dynamically ?
Regards
i want to calculate the total costs of products selected by a user under each company.There is my code.
let companiesProductPrices = [];
let prices = [];
this.selectedCompaniesDetails.forEach(company => {
this.chosenItems.forEach(item=> {
item.product_attributes.forEach(product => {
if(company.id == product.company_id)
{
prices.push(product.price);
companiesProductPrices[company.id] = prices;
}
})
});
})
Background information. I have to objects. The products object and the companies object.
Products object referred to as this.chosenItems in my code and is like this;
[
{
"id": 1,
"name": "Zimgold",
"slug": null,
"product_category_id": 1,
"industry_id": 1,
"category": {
"id": 1,
"name": "Cooking Oil",
"slug": null,
"industry_id": 1
},
"product_attributes": [
{
"id": 1,
"description": "2 litres",
"discount": null,
"price": "120",
"tax": null,
"company_id": 1,
"product_id": 1
},
{
"id": 5,
"description": "2 litres",
"discount": null,
"price": "130",
"tax": null,
"company_id": 2,
"product_id": 1
}
]
},
{
"id": 4,
"name": "Silo",
"slug": null,
"product_category_id": 3,
"industry_id": 1,
"category": {
"id": 3,
"name": "Mealie Meal",
"slug": null,
"industry_id": 1
},
"product_attributes": [
{
"id": 4,
"description": "10 kgs",
"discount": null,
"price": "130",
"tax": null,
"company_id": 1,
"product_id": 4
}
]
}
]
and my company object referred to as this.selectedCompaniesDetails in my code and is like this.
[
{
"id": 1,
"name": "Spar",
"slug": null,
"industry_id": 1
},
{
"id": 2,
"name": "Ok",
"slug": null,
"industry_id": 1
},
{
"id": 3,
"name": "TM",
"slug": null,
"industry_id": 1
},
{
"id": 4,
"name": "choppies",
"slug": null,
"industry_id": 1
},
{
"id": 5,
"name": "Bon Marche",
"slug": null,
"industry_id": 1
}
]
I'm wishing to get the totals of all products that are belong to each company. Let me show you my UI.
So what i want is under spar the total should be 250 and under 130 and so forth for all other companies. Thank you in advance
This is one of many solutions. It's improvable, it's just to show you the way.
#1 Create an object where key is company id and value is the array of prices for that company
const priceByCompany = products.reduce((tot, prod) => {
prod.product_attributes.forEach((p) => {
if (!tot[p.company_id]) {
tot[p.company_id] = []
}
const numberPrice = parseInt(p.price, 10)
tot[p.company_id].push(numberPrice)
})
return tot
}, {})
// { 1: [120, 130], 2: [130] }
#2 Reduce all value array to sum them up.
Object.keys(priceByCompany).forEach((company) => {
const total = priceByCompany[company].reduce((tot, price) => tot + price, 0)
priceByCompany[company] = total
})
// { 1: 250, 2: 130 }
See the code in action: Jsfiddle
I have json stored in a column (oid) with the following structure:
{
"fullName": "test test",
"personDetails": {
"address": "Advisor",
"phoneNumber": "clare.railton#heptonstalls.co.uk"
},
"id": "6765788-yt67",
"submittedDocument": {
"answers": [
{
"questionId": "2",
"responses": [
{
"value": "123456"
}
]
},
{
"questionId": "2.1",
"responses": [
{
"IdA": 1,
"IdB": 1,
"value": "false"
},
{
"IdA": 1,
"IdB": 2,
"value": "false"
},
{
"IdA": 1,
"IdB": 3,
"value": "false"
},
{
"IdA": 1,
"IdB": 4,
"value": "true"
}
]
}
]
},
"date": "2018-11-22",
"PeriodId": 123456
}
How would i get the value of the response to all question numbers? I have managed to get the json structure from the oid column using the lo_get function but i am struggling to capture the values i need.
Many thanks
Are you sure you want a large object to store the json data?
Postgres can handle json columntypes in tables:
CREATE TABLE test_json (id INTEGER PRIMARY KEY, json json);
INSERT INTO test_json VALUES(1,'{
"fullName": "test test",
"personDetails": {
"address": "Advisor",
"phoneNumber": "clare.railton#heptonstalls.co.uk"
},
"id": "6765788-yt67",
"submittedDocument": {
"answers": [
{
"questionId": "2",
"responses": [
{
"value": "123456"
}
]
},
{
"questionId": "2.1",
"responses": [
{
"IdA": 1,
"IdB": 1,
"value": "false"
},
{
"IdA": 1,
"IdB": 2,
"value": "false"
},
{
"IdA": 1,
"IdB": 3,
"value": "false"
},
{
"IdA": 1,
"IdB": 4,
"value": "true"
}
]
}
]
},
"date": "2018-11-22",
"PeriodId": 123456
}');
SELECT json_extract_path(
json_array_elements(
json_extract_path(
json_array_elements(
json_extract_path((json),'submittedDocument','answers')
),'responses')
),'value'
)FROM test_json;
See:
https://www.postgresql.org/docs/current/functions-json.html
I have seen several examples to export MySQL tables to JSON, however such examples export data in an aggregated way. For example, take a database where you have two tables "Invoice head" and "Invoice details". "Invoice details" is a child of "Invoice head".The data in JSON is usually represented like:
{
"invoice_head": [
{
"number": 1,
"cliente": "Carlos",
"date": "2016-12-12"
},
{
"number": 2,
"cliente": "Fernando",
"date": "2017-01-01"
}
],
"invoice_details": [
{
"headnumber": 1,
"lineno": 1,
"product": "Shoes",
"quantity": 2
},
{
"headnumber": 1,
"lineno": 2,
"product": "Socks",
"quantity": 1
},
{
"headnumber": 2,
"lineno": 1,
"product": "Laptop",
"quantity": 1
}
],
}
I need to export data in a nested way:
{
"invice_head": [
{
"number": 1,
"cliente": "Carlos",
"date": "2016-12-12",
"invice_details": [
{
"headnumber": 1,
"lineno": 1,
"product": "Shoes",
"quantity": 2
},
{
"headnumber": 1,
"lineno": 2,
"product": "Socks",
"quantity": 1
}
]
},
{
"number": 2,
"cliente": "Fernando",
"date": "2017-01-01"
"invice_details": [
{
"headnumber": 2,
"lineno": 1,
"product": "Laptop",
"quantity": 1
}
]
}
]
}
For this I guess one needs to start in a table and recursively go through its childs for each record.
Does anybody knows if there is anything that does it? I don't want to reinvent the wheel.
I'm working on some code in which uses dynamic variables jsonResponse .
dynamic jsonResponse = JsonConvert.DeserializeObject(response);
This variable contains collection of hotel list in json format. From this collection I am getting roomlist collection in a new variable roomResponseList :
var roomResponseList = jsonResponse["hotels"]["hotels"][rooms].roomResponseList;
I am getting first room detail into **JObject responseRateKeys **:
foreach (var roomByResponse in roomResponseList)
{
JObject responseRateKeys = JObject.Parse(roomByResponse.ToString());
var boardNameListByResponse = responseRateKeys.AsJEnumerable().AsEnumerable()
.Select(t => t["rates"]["boardName"].ToString().Trim())
.Distinct()
.ToList();
}
But when I am trying to get any item list from JObject by using linq lambda, I am getting error,
"Cannot access child value on Newtonsoft.Json.Linq.JProperty."
Value of roomByResponse=
{ "code": "DBL.KG-NM", "name": "DOUBLE KING BED NON SMOKING", "rates": [ { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|1~1~0||N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "186.04", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "149.63", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "93.02" }, { "offset": 2, "dailyNet": "93.02" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|1~1~0||N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "238.92", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "119.46" }, { "offset": 2, "dailyNet": "119.46" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|2~2~1|2|N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "372.06", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "299.25", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "186.03" }, { "offset": 2, "dailyNet": "186.03" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|2~2~1|2|N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "477.84", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "238.92" }, { "offset": 2, "dailyNet": "238.92" } ] } ] }
Thank you
Pravesh Singh
change linq to
responseRateKeys["rates"].AsJEnumerable().Select(t=>t["boardName"]).Distinct().ToList()