I have a column in my data base called extra_fields, it's of the jsonb type, I don't know what might be in there, since my user can send unlimited data, but I wanted to retrieve the keys and map this column. The problem is that I need it to be multidimensional (nested), is it possible in postgres?
Example of column value:
Row 1
{
"name": "John Doe",
"address":{
"city": "London",
"country": "UK"
},
"status": [{
"value": "Inactive",
"inserted_at": "2050-01-02 12:00:00"
},{
"value": "Active",
"inserted_at": "2050-01-01 12:00:00"
}]
}
Row 2
{
"name": "John Doe",
"phone": "+345 43456576"
"address":{
"city": "London",
"country": "UK"
},
"status": [{
"value": "Inactive",
"inserted_at": "2050-01-02 12:00:00"
},{
"value": "Active",
"inserted_at": "2050-01-01 12:00:00"
}]
}
And i need to return the keys like this:
[name, phone, address: [city, country], status: [value, inserted_at]]
Any suggestions on how I can do this?
My stacks are nestjs, typeorm and postgres.
Related
I have a collection of countries and for each document of it, I have a collection of cities.
I want to get all countries and their cities like the array below.
I am using NodeJs SDK.
[
{
"country": {
"codeISO3166": "NL",
"name": "Netherlands",
"id": "47891a32-6eb3-4516-9ca1-b1ae16f9fcd3",
"isEnable": true
},
"cities": [
{
"name": "Amsterdam",
"id": "a2394abb-937c-4ba1-92cd-c2ffa7f8d3b2",
"isEnable": true
},
{
"name": "Utrecht",
"id": "2dcae8be-4ef3-4f2f-a594-a0019b2c234a",
"isEnable": true
}
]
},
{
"country": {
"codeISO3166": "UK",
"name": "United Kingdom",
"id": "05236cab-dfdf-47ba-b7ed-b34fe6320183",
"isEnable": true
},
"cities": [
{
"name": "London",
"id": "a47a7c71-b0a2-4ba4-9170-2a5a9fed3757",
"isEnable": true
},
{
"name": "Liverpool",
"id": "e6ac7fd7-aba3-45ab-8e13-e29f7dc35b04",
"isEnable": true
}
]
}
]
There is no way to get a country and all its cities with one read operation.
Reads in Firestore are shallow, and only return documents from one collection or from all collections with a given name.
The closest you can get is:
one read for all documents in the countries collection
one collection group query for all documents in all cities subcollections.
I have a problem with an object containing an array in its view on a table. I want where I have an array in the column. It will split it.
For example
itemsToSend = "Orders": [
{
"date": {
"$date": "2022-06-09"
},
"OrderNumber": "333333",
"City": "xxxxxx",
"Address": "yyyyyyy",
"Phone": "6666666",
"Name": "xxxxxx",
"Trackingnumber": "1",
"ZipCode": 00000,
"Province": "xxxx",
"Quantity": [
2,
1
],
"Product_Name": [
"Nine pants 1",
"Nine pants 2"
],
"SKU": [
"CJNSXZHL",
"CJNS"
],
"_id": {
"$oid": "62a1fc431d9acc4a65df50e1"
}
},
{
"date": {
"$date": "2022-06-09"
},
"OrderNumber": "7488404",
"City": "Colorad",
"Address": " xxx",
"Phone": "3232323",
"Name": "xxxxxx",
"Trackingnumber": "1",
"ZipCode": ooooo,
"Province": "yyyyy",
"Quantity": [
1
],
"Product_Name": [
"Feet harem pants "
],
"SKU": [
"CJNSXZHL"
],
"_id": {
"$oid": "62a1fc431d9acc4a65df50e3"
}
}
]
Now I do to the object map And sends it by props
<>
{itemsToSends.map((itemsToSend,i)=>(
<Posreturn key ={i} post={itemsToSend}
/>
<tr>
<td>{post.OrderNumber}</td>
<td>{post.Name }</td>
<td>{post.SKU}</td>
<td>{post.Phone}</td>
<td>{post.Address}</td>
<td>{post.Country}</td>
<td>{post.Province}</td>
<td>{post.ZipCode}</td>
<td>{post.Quantity}</td>
</tr>
You can see in the first array there is in Quantity, there is an array
And when I display it in the table, I get it in one unsplit cell. Is there any way to split the cell by array
I added a screenshot of what I get
screenshot
You can see that the first line is incorrect
Both in SKU and in quantity
Thanks for the helpers
I'm currently trying to code an application with javascript. It pulls data from a database and the response I'm getting is something like that:
{
"values":[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
}
But I need to have the JSON like that:
[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
How can I delete the parent values object in my JSON?
SHORT ANSWER:
Just access the values property like a JavaScript object.
LONG ANSWER:
You didn't post the JavaScript code snippet so it's quite difficult to give you an appropriate answer.
Assuming you have the following code:
const jsonString = getDataFromTheDB()
const jsonObject = JSON.parse(jsonObject) // still has the "values" layer
const values = jsonObject.values // what you want, without the "values" layer
// BONUS: Just in case you want to convert the object back to a JSON string but without the "values" layer
const valuesJSON = JSON.stringify(values, undefined, 2)
Based on this post :
just do this (consider json the variable that contains your json):
var key = "values";
var results = json[key];
delete json[key];
json = results;
console.log(json) will output the following:
[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
But you dont even have to do the last 2 steps of the code snippet above, you could also just directly use results variable and have the same output by console.log(results).
You could take the object and create a new variable with just the array.
var vals =
{
"values":[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
}
var arr = vals.values;
console.log(arr);
[{
"name": "employeeOne",
"age": 22,
"position": "UI",
"city": "Chennai"
},
{
"name": "employeeTwo",
"age": 23,
"position": "UI",
"city": "Bangalore"
}
]
If I delete "Position"
key & value from the json the result should be like[
[{
"name": "employeeOne",
"age": 22,
"city": "Chennai"
}, {
"name": "employeeTwo",
"age": 23,
"city": "Bangalore"
}]
If we have list of employee in a table I want to delete all the employees position column only. How can I achieve this with JavaScript and Typescript
I tried
const output = delete employee.position
but getting an error.
var data = [{
"name": "employeeOne",
"age": 22,
"position": "UI",
"city": "Chennai"
},
{
"name": "employeeTwo",
"age": 23,
"position": "UI",
"city": "Bangalore"
}
];
data.forEach( item => {delete item.position;});
console.log(data);
You have an array of objects there. You would have to loop through the array, deleting the property in each object individually.
take a look at the delete operation
pseudo:
foreach obj in jsonArray
{
delete obj["position"];
}
also works:
obj[position]
obj.position
http://perfectionkills.com/understanding-delete/
I can't figure out how I can count the total devices based on location and unique serial number.
{
"dates": [
{
"date": "Sep 1, 2014",
"devices": [
{
"model": "Canon",
"location": "Chicago",
"serialNum": "abc123"
},
{
"model": "Canon",
"location": "Chicago",
"serialNum": "xyz456"
},
{
"model": "HP",
"location": "New York",
"serialNum": "123456"
},
{
"model": "Brother",
"location": "Chicago",
"serialNum": "DEF777"
}
]
},
{
"date": "Sep 2, 2014",
"devices": [
{
"model": "Canon",
"location": "Chicago",
"serialNum": "abc123"
},
{
"model": "Canon",
"location": "Chicago",
"serialNum": "xyz456"
}
]
},
{
"date": "Sep 3, 2014",
"devices": [
{
"model": "Canon",
"location": "Chicago",
"serialNum": "xyz456"
},
{
"model": "Canon",
"location": "Chicago",
"serialNum": "stu789"
},
{
"model": "Epson",
"location": "NewYork",
"serialNum": "123456"
},
{
"model": "Epson",
"location": "NewYork",
"serialNum": "555555"
},
{
"model": "HP",
"location": "NewYork",
"serialNum": "987654"
}
]
}
]
}
I would like to capture the total number of unique devices per location
Chicago - 4
New York - 3
Here it is, in a single expression with lodash chaining syntax. The short version is you get all devices into one massive list, then you group them by location, then for each location eliminate the duplicate ids, then count the devices.
_(dates) //Begin chain
.pluck("devices") //get the device list for each date
.flatten() //combine all device lists into a master list
.groupBy("location") //group into an object of {"location": [devices]} pairs
.mapValues(function (devicesForLocation) { //replaces [devices] with a count of number of unique serial numbers
return _(devicesForLocation) //Begin chain
.pluck("serialNum") //get the serial number for each device
.uniq() //remove the duplicate serial numbers
.value() //End chain
.length; // get the count of unique serial numbers
}) // result is an object of {"location": countOfUniqueDevices} pairs
.value() //End chain
The final result is an object of the form: {"New York": 1, "NewYork": 3, "Chicago": 4}, though you could add another statement to print that out in string form, obviously.
I'd encourage you to run through this step-by-step to see the result of each step and understand what it's doing.