I have this specific JSON file I'm not allowed to change, which looks like this :
{
"computers" : {
"John" : {
"version" : "1.42.0",
"environment" : "Default",
"platform" : "x64",
"admin" : "true"
},
"Peter" : {
"version" : "1.43.6",
"environment" : "Default",
"platform" : "x64",
"admin" : "true"
},
"Eric" : {
"version" : "1.43.6",
"environment" : "Default",
"platform" : "x64",
"admin" : "false"
}
}
I use the JSON.parse() method to parse the file and I put it into a MatTableDataSource.
Problem is, when I need to display it in my MatTable, I can't really access it the way I want.
I have a column where I want to display all version parameters, so for this I can't say : this.dataSource.computers.????.version
Do you guys see where I'm getting at ? Do you have any idea of what I can do differently in order to solve this ?
Looking forward to reading you.
The Angular mat-table requires the input data to be in an array. First, we use Object.keys() to extract the keys, which will contain the list of names. Then, we can use Object.values() to other values within each key in array format. This is followed by mapping the above array objects with the name property from the list of names.
const data = {
"computers": {
"John": {
"version": "1.42.0",
"environment": "Default",
"platform": "x64",
"admin": "true"
},
"Peter": {
"version": "1.43.6",
"environment": "Default",
"platform": "x64",
"admin": "true"
},
"Eric": {
"version": "1.43.6",
"environment": "Default",
"platform": "x64",
"admin": "false"
}
}
};
const nameList = Object.keys(data.computers);
const dataList = Object.values(data.computers).map((obj, index) => {
obj['name'] = nameList[index];
return obj;
});
console.log(dataList);
Related
I've been working with Firebase, and in a part of my JSON tree, I had a bunch of consecutive numbers as keys.
Here's an abbreviated portion of that part of the tree:
"matches" : {
"1" : {
"0931-Red" : [
"John Smith"
],
"2022-Blue" : [
"Paul Adams"
]
},
"2" : {
"1489-Red" : [
"Matthew Brown"
],
"1565-Blue" : [
"Ian Fowler"
]
},
"3" : {
"1652-Red" : [
""
],
"2626-Blue" : [
""
]
}
}
When I downloaded it from Firebase and used this:
if let r = ref {
r.child(accessKey).child("matches").observeSingleEvent(of: .value, with: { (snapshot) in
if let groups = snapshot.value as? NSDictionary {
The conversion to NSDictionary failed.
However, when I edited "matches" to look like this:
"matches" : {
"1" : {
"0931-Red" : [
"John Smith"
],
"2022-Blue" : [
"Paul Adams"
]
},
"2" : {
"1489-Red" : [
"Matthew Brown"
],
"1565-Blue" : [
"Ian Fowler"
]
},
"4" : {
"1652-Red" : [
""
],
"2626-Blue" : [
""
]
}
}
It worked.
I was able to convert the data to an NSDictionary.
This also worked by changing the first "1" to "01" and appending a string to the beginning of each number like "match-1", "match-2", etc.
Even though my code works, the error is still bothering me because I don't know why it was wrong in the first place.
Why didn't it work with consecutive numbers as keys in the first place?
Thanks in advance!
I'm new to Mongoose and I've been trying for days on how to solve this issue and I'm still having trouble.
My document object is below.
"person" : [
{
"title" : "front-end developer",
"skills" : [
{
"name" : "js",
"project" : "1",
},
{
"name" : "CSS",
"project" : "5",
}
]
},
{
"title" : "software engineer",
"skills" : [
{
"name" : "Java",
"project" : "1",
},
{
"name" : "c++",
"project" : "5",
}
]
}
]
What I would like accomplish is to return all documents that have person.title = software engineer AND person.skills.name = c++. The skill c++ has to belong to the software engineer person object. So returning documents when a front-end developer has c++ is not ideal.
Here's what I've tried doing so far. The query works but it returns documents which meet either one of the conditions and not both.
var query = {
_id: { $nin: [userID] },
$and: [
{person: {
$elemMatch: {
name: {$regex: `^${titleName}$`, $options: "i"}
}
}},
{[`person.skills`]: {
$elemMatch: {
name: {$regex: `^${skillName}$`, $options: "i"}
}
}}
]
};
Any help would be greatly appreciated. Thanks!
You can try below query. Move the and condition inside the $elemMatch
var query = {
"_id": {
"$nin": [userID]
},
"person": {
"$elemMatch": {
"name":{$regex: `^${titleName}$`, $options: "i"},
"skills.name": {$regex: `^${skillName}$`, $options: "i"}
}
}
};
Need a JSON path expression for below JSON. I wanted to extract "Id" for each specific "name"
For Example: I need to extract "Id" : "3" for "name" : "XYZ" .
I tried a JSON path expression as $..Id which given output as:
[
"1",
"2",
"3"
]
But I needed an Id specific to "name": "XYZ"`
[
{
"primary":{
"name":"ABC"
},
"Id":"1"
},
{
"primary":{
"name":"PQR"
},
"Id":"2"
},
{
"primary":{
"name":"XYZ"
},
"Id":"3"
}
]
Able to resolve this by below expression
$..[?(#.primary.name == 'XYZ')].Id
How to fetch the data from the json file using mongoshell
I want to fecch the Data by policyID
Say in the json file I sent the PolicyID is 3148
I tried could of ways to write the command but say 0 rows fetched.
db.GeneralLiability.find({"properties.id":"21281"})
db.GeneralLiability.find({properties:{_id:"21281"}})
Do i need to set any thing else?index,cursors etc?
Sample json
{
"session": {
"data": {
"account": {
"properties": {
"userName": "abc.com",
"_dateModified": "2014-10-01",
"_manuscript": "Carrier_New_Rules_2_1_0",
"_engineVersion": "2.0.0",
"_cultureCode": "en-US",
"_cultureName": "United States [english]",
"_context": "Underwriter",
"_caption": "Carrier New Rules (2.1.0)",
"_id": "p1CEB08012E51477C9CD0E89FE77F5E51"
},
"properties": {
"_xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"_id": "3148",
"_HistoryID": "5922",
"_Type": "onset",
"_Datestamp": "2014-10-01T04:46:33",
"_TransactionType": "New",
"_EffectiveDate": "2014-01-01",
"_Charge": "1599",
"_TransactionGroup": "t4CE4FA751F9C400D9007E692A883DA66",
"_PolicyID": "3148",
"_Index": "1",
"_Count": "1",
"_Sequence": "1"
}
}
}
This will return the document with _PolicyID = "3148":
db.GeneralLiability.find({
"session._PolicyID": "3148"
}).pretty();
You have some issues in your document formatting. First off I am pretty sure that using underscores are reserved for mongo (I could be wrong). Either way it is bad form. I have restructured your data for you. I am not sure why you wanted to nest your data so much, but I am guessing you had a good reason for it.
You will notice that I am using the ObjectID from Mongo for my _id:
{
"_id" : ObjectId("56e1c1f53bac31a328e3682b"),
"session" : {
"data" : {
"account" : {
"properties" : {
"xmlns:xsd" : "http://www.w3.org/2001/XMLSchema",
"xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
"HistoryID" : "5922",
"Type" : "onset",
"Datestamp" : "2014-10-01T04:46:33",
"TransactionType" : "New",
"EffectiveDate" : "2014-01-01",
"Charge" : "1599",
"TransactionGroup" : "t4CE4FA751F9C400D9007E692A883DA66",
"PolicyID" : "3148",
"Index" : "1",
"Count" : "1",
"Sequence" : "1"
}
}
}
}
}
Now if you run this command it will return your document:
{ "session.data.account.properties.PolicyID": "3148" }
I have the following json object:
{
"Title": "Terminator,
"Purchases": [
{"Country": "US", "Site": "iTunes", "Price": 4.99},
{"Country": "FR", "Site": "Google", "Price": 5.99}
]
}
I want to be able to find an object specifying a Country+Site+PriceRange. For example, the above should return True on Country=US&Price<5.00, but should return False on Country=FR&Price<5.00. How would the index and query look to do this? Here is another answer that this is a follow-up question to: Search within array object.
Simply add a Range query to your Bool query logic tree. This will return documents that match US for country and have the Price field with a numeric value less than 5.
{ "query":
{ "nested" : {
"path" : "Purchases",
"score_mode" : "avg",
"query" : {
"bool" : {
"must" : [
{
"match" : {"Purchases.Country" : "US"}
},
{
"range" : "Purchases.Price":
{
"lte": 5
}
}
]
}
}
}
}
}