Here is the my JSON Array format
[
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
]
The "1234","4567" and "7890" are my Ids and "Name11","Name12", "Name13", "Name21", "Name22" and "Name23" are the values for each corresponding Ids.
The JSON array contains two records as shown above. Now i need to convert the following JSON Format....
[
{
"1234":"Name11",
"4567":"Name12",
"7890":"Name13"
},
{
"1234":"Name21",
"4567":"Name22",
"7890":"Name23"
}
]
Please help me out, how can i construct the above mentioned JSON array format.
With pure javascript:
var src = [
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
]
var newObj = []
for(var i = 0; i< src.length; i++){
var item = {}
for(var j = 0; j<src[i].length; j++){
if(j%2 === 0){
item[src[i][j]] = ""
}else{
item[src[i][j-1]] = src[i][j][0]
}
}
newObj.push(item)
}
You can do it with the following code (see the snippet below):
var res = src.map(function(a){
var arr = [];
for(var i=0;i<a.length/2;i++) {
var item = {};
item[a[i*2]] = a[i*2+1][0];
arr.push(item);
};
return arr;
});
var src = [
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
];
var res = src.map(function(a){
var arr = [];
for(var i=0;i<a.length/2;i++) {
var item = {};
item[a[i*2]] = a[i*2+1][0];
arr.push(item);
};
return arr;
});
document.getElementById("res").textContent = JSON.stringify(res);
<div id="res"></div>
Related
I need to convert the following array data to Json in react.
I tried map method, but it is not the correct way. I need key value pair, so that i can pass it to server as json
[
[
"channel",
"s1"
],
[
"category",
"Account 1"
],
[
"accountAdministration",
"Partnership 1"
],
[
"partnershipAccounting",
"1 level Performance issues"
],
[
"requestCategory",
"Research"
],
[
"severity",
"Blocker"
],
[
"activationDate",
"2020-10-29T05:54:00.000Z"
],
[
"managerApproved",
true
]
]
Try using reduce and creating an object:
var arr = []; // your array
var data = arr.reduce((map, item) => {
map[item[0]] = item[1];
return map;
}, {});
The data object will be in the following format:
{
"accountAdministration": "Partnership 1",
"activationDate": "2020-10-29T05:54:00.000Z",
"category": "Account 1",
"channel": "s1",
...
}
Worked with
Object.fromEntries([
[
"channel",
"s1"
],
[
"category",
"Account 1"
],
[
"accountAdministration",
"Partnership 1"
],
[
"partnershipAccounting",
"1 level Performance issues"
],
[
"requestCategory",
"Research"
],
[
"severity",
"Blocker"
],
[
"activationDate",
"2020-10-29T05:54:00.000Z"
],
[
"managerApproved",
true
]
])
I am trying to query documents from a mongodb collection, based on array of input query parameters sent from URL.
Sample Database Data
[
{
"drawings": {
"circle": [],
"square": [
{
"id": "828",
"name": "square"
}
],
"cube": []
},
{
"drawings": {
"circle": [
{
"id": "827",
"name": "circle"
}
],
"square": [],
"cube": []
},
{
"drawings": {
"circle": [],
"square": [],
"cube": [
{
"id": "829",
"name": "cube"
}
]
}
]
Input Query Parameter:
query = ["square","cube"];
Expected Output:
[
{
"drawings": {
"circle": [],
"square": [
{
"id": "828",
"name": "square"
}
],
"cube": []
},
{
"drawings": {
"circle": [],
"square": [],
"cube": [
{
"id": "829",
"name": "cube"
}
]
}
]
Best suited Mongoose Query:
Schema.find({
$or:[
{'drawings.square':{$elemMatch:{ name:'square'}}},
{'drawings.cube':{$elemMatch:{ name:'cube'}}}
]
});
Tried Below method. But, it is not correct.
let draw = ["square","cube"];
let draw_query =[];
for (let a=0; a<draw.length;a++){
draw_query.push("{\"drawings."+ draw[a] +"\':{$elemMatch:{ name:\"" + draw[a] + "\"}}}");
}
It creates array with single quoted strings. It cannot be used.
[ '{"drawings.square":{$elemMatch:{ name:"square"}}}',
'{"drawings.cube":{$elemMatch:{ name:"cube"}}}' ]
How to generate this mongoose query dynamically? or is there any better mongoose query to achieve the expected result.
You can query it directly using dot notation so the query should look like below:
db.collection.find({
$or: [
{
"drawings.square.name": "square"
},
{
"drawings.circle.name": "circle"
}
]
})
You can build it in JS using .map(), try:
var query = ["square","cube"];
var orQuery = { $or: query.map(x => ({ [x + ".name"]: x }) ) }
I'm trying to parse JSON which is like below:
"price": [
[
1539283140000,
6288.07
],
[
1539283440000,
6285.82
],
[
1539283740000,
6285.81
],
[
1539284041000,
6280.37
],
[
1539284340000,
6280.19
]
Please help me deal with this. And is there a possibility to decode the timestamp value to a date.
Correct json
{
"price": [
[
1539283140000,
6288.07
],
[
1539283440000,
6285.82
],
[
1539283740000,
6285.81
],
[
1539284041000,
6280.37
],
[
1539284340000,
6280.19
]
]
}
struct Root: Codable {
let price: [[Double]]
}
let res = try? JSONDecodable().decode(Root.self,from:jsonData)
I wannt to access the fields in my JSONArray. The nested brackets inside the JSONArray is very troublesome. I can't see how this format is an acceptable JSONArray return value. I get an JSONException when I try to access a field (eg "rethink3__Address__c") using getJSONObject().
[
[
{
"attributes":{
"type":"rethink3__Listing__c",
"url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
},
"rethink3__Address__c":null,
"Alarm_Code__c":null,
"rethink3__Bathrooms__c":0,
"rethink3__Bedrooms__c":0,
"rethink3__Size__c":0,
"Lock_Box_Code__c":null,
"Lock_Box_Location_Notes__c":null,
"_soupEntryId":1,
"_soupLastModifiedDate":1537657104801
}
],
[
{
"attributes":{
"type":"rethink3__Listing__c",
"url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
},
"rethink3__Address__c":null,
"Alarm_Code__c":null,
"rethink3__Bathrooms__c":0,
"rethink3__Bedrooms__c":0,
"rethink3__Size__c":0,
"Lock_Box_Code__c":null,
"Lock_Box_Location_Notes__c":null,
"_soupEntryId":1,
"_soupLastModifiedDate":1537657104801
}
]
]
A [] = json array and {} = json object. So try this.
let myArray = [
[
{
"attributes":{
"type":"rethink3__Listing__c",
"url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
},
"rethink3__Address__c":null,
"Alarm_Code__c":null,
"rethink3__Bathrooms__c":0,
"rethink3__Bedrooms__c":0,
"rethink3__Size__c":0,
"Lock_Box_Code__c":null,
"Lock_Box_Location_Notes__c":null,
"_soupEntryId":1,
"_soupLastModifiedDate":1537657104801
}
],
[
{
"attributes":{
"type":"rethink3__Listing__c",
"url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
},
"rethink3__Address__c":null,
"Alarm_Code__c":null,
"rethink3__Bathrooms__c":0,
"rethink3__Bedrooms__c":0,
"rethink3__Size__c":0,
"Lock_Box_Code__c":null,
"Lock_Box_Location_Notes__c":null,
"_soupEntryId":1,
"_soupLastModifiedDate":1537657104801
}
]
];
myArray.forEach((myNestedArray)=>{
let obj = myNestedArray[0]
console.log(obj.attributes.type);
console.log(obj._soupLastModifiedDate);
})
I want to create ,multidimensional array in swift with multiple inner arrays. I want the structure look like this but getting error while creating this. Please guide me how can I create this structure.
class ViewController: UIViewController
{
var data2:[String] = []
var LocationPickerData = ["Mozzarella","Gorgonzola","Provolone","Brie","Maytag Blue"]
var LocationPickerData2 = ["Sharp Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]
var LocationPickerData3 = ["Goat Cheese", "Asiago"]
var Title = ["twwwitle1","sdfsaf","Prsdfasovosdfasflone","sfsa","sdfsf Blue"]
var Title2 = ["seeefdasf Cheddar","sdfsf Jack","fsaf","Gouda","fasf Cheese", "sdfsf"]
var Title3 = ["ddddsdfs ff", "fasfsdffasf"]
var Name = ["fsdfa","fasfsafsf","Provolone","Brie","Maytag Blue"]
var Name2 = ["afsadff Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]
var Name3 = ["fffffff ffff", "jjjjjjj"]
var data2 = [
location[
location1[ LocationPickerData ],
location2[ LocationPickerData2],
location3[ LocationPickerData3]
],
titles[
title1[Title],
title2[Title2],
title3[Title3]
],
names[
name1[Name],
name2[Name2],
name3[Name3]
]
]
}
Declare your multidimensional array like this:
var data2 = [
[
LocationPickerData
LocationPickerData2,
LocationPickerData3,
],
[
Title
Title2,
Title3,
],
[
Name
Name2,
Name3,
]
]
Or you can declare a dictionary:
var data2 = [
"Locations": [
LocationPickerData
LocationPickerData2,
LocationPickerData3,
],
"Titles": [
Title
Title2,
Title3,
],
"Names": [
Name
Name2,
Name3,
]
]