I have a JSON array, which can contain several different elements in it - the number is unknown to me.
JSON:
[{
"_id": "5911a43e6aa5d609d32c590a",
"criteria": "3",
"name": "test1"
}, {
"_id": "5911a43e6aa5d609d32c590d",
"criteria": "7",
"name": "test2"
}, {
"_id": "5911a43e6aa5d609d32c5910",
"criteria": "2",
"name": "test3"
}]
I need to convert this data into XML, so that it is in the following structure:
<MyDefs criteria="3">
<name>test1</name>
</MyDefs>
<MyDefs criteria="7">
<name>test3</name>
</MyDefs>
<MyDefs criteria="2">
<name>test2</name>
</MyDefs>
I tried using xml2js node library- I assume I need a foreach loop but not sure how to implement - is there a way to achieve this?
Thank you
Related
I have a collection in my MongoDB that contains one field that is an array.
Refer to the data above, the field 'Courses' is an array.
The JSON format of the data is like this:
{
"_id": {
"$oid": "60eb59b98a970a20865142e8"
},
"Name": "Sadia",
"Age": 24,
"Institute": "IBA",
"Courses": [{
"Name": "ITP",
"Grade": "A-"
}, {
"Name": "OOP",
"Grade": "A-"
}]
}
I am aware that there is a way in case its an object, but could not find a way on how to read this data using Talend since it contains an array.
I have a file1.json with structure like this :
[
{
"uri": "features/hdp.feature",
"id": "as-a-user-i-want-to-use-house-detailed-page",
"keyword": "Feature",
"name": "As a user I want to use house detailed page",
"description": "",
"line": 2,
"tags": [
{
"name": "#hdp",
"line": 1
}
],
"elements": [
{
As you can see - it is an array with nested key:value pairs and other arrays. I need to convert it to ruby hash, but when I'm performing JSON.parse(file1) - it creates an array (http://prntscr.com/lqio6r) with ruby hashes, arrays and so on. If I'm performing JSON.parse(file1).reduce Hash.new, :merge or JSON.parse(file1).reduce Hash.new, :update) - as one of the answers on StackOverflow supposed - the result hash losses about 60% of .json content. Can you please advice on how can I convert json file to ruby hash (without any data losses)?
UPD - not truncated array - https://gist.githubusercontent.com/M1khah/3337507e3ca1544e6098bc726bca90cb/raw/c8262ad753bd0eebf1180e111acd016ffc07d1a5/gistfile1.txt
Hash with hashes - something like this instead of an array with nested hashes
{
{
"uri": "features/hdp.feature",
"id": "as-a-user-i-want-to-use-house-detailed-page",
"keyword": "Feature",
"name": "As a user I want to use house detailed page",
"description": "",
"line": 2,
"tags": [
{
"name": "#hdp",
"line": 1
}
],
"elements": [
{
}
I have some restmethods with powershell (VMWare vRA API) where I get a template for a development request and then I need to fill that (JSON format).
The "data" part of the template has different properties like:
Name : Test
selectedNetworks :
selectedServices :
My question:
How do I get my array $networks (network1,network2,network3) to fill the json element "selectedNetworks" that it becomes like this:
"key": "selectedNetworks",
"value": {
"type": "multiple",
"elementTypeId": "STRING",
"items": [
{
"type": "string",
"value": "network1"
},
{
"type": "string",
"value": "network2"
},
{
"type": "string",
"value": "network3"
}
]
}
I know how to add in "simple" values like the name is
$WebRequestBodyData.Name="$Name"
But how do I get the complex format above into $WebRequestBodyData.selectedNetworks?
Any help is greatly appreciated!
Thanks and best regards,
Ville
I could solve it like that:
$networkjson = '{"type": "multiple", "elementTypeId": "STRING", "items": [] }' | ConvertFrom-Json
foreach($nwitem in $networks){
$networkjson.items += $nwitem
}
$BodyData.selectedNetworks += $networkjson
I am trying to identify documents where both key-value pairs within an array match using the aggregate pipeline. Specifically, if I want to find documents where one array contains user_attribute.Name = Quests_In_Progress and user_attribute.Value =3. Below is an example of such a document that I'm trying to match.
If I use
db.myCollection.aggregate({
$match: {
"user_attribute.Name": "Quests_In_Progress",
"user_attribute.Value": "3"
}
})
It will match every document that contains Quests_In_Progress for user_attribute.Name in one element of the array and contains "3" for user_attribute.Value, regardless of whether they exist in the same element of the array or not.
i.e.
db.myCollection.aggregate({
$match: {
"user_attribute.Name": "Quests_In_Progress",
"user_attribute.Value": "0"
}
})
will match the same document simply because one element of the array has a key:Value pair of Value:0 and another element of the array contains a key:value pair of Quests_In_Progress.
What I want to do is identify documents where both of those conditions are met within one element of the array.
I tried to do this with $elemMatch, but I couldn't get it to work. Plus the aggregate documentation doesn't indicate that $elemMatch works, so maybe that's why I couldn't get it to work.
Lastly, I need to use the aggregate pipeline, because there are a bunch of other things I have to do after finding these documents- specifically unwinding them.
{
"_id": ObjectId("5555bb32de938ce667f78ce00"),
"user_attribute": [{
"Value": "Facebook",
"Name": "Social_Connection"
}, {
"Name": "Total_Fireteam_Missions_Initiated",
"Value": "0"
}, {
"Name": "Quests_Completed",
"Value": "3"
}, {
"Name": "Item_Slots_Owned",
"Value": "36"
}, {
"Name": "Quests_In_Progress",
"Value": "3"
}, {
"Name": "Player_Progression",
"Value": "0"
}, {
"Value": "1",
"Name": "Characters_Owned"
}, {
"Name": "Quests_Started",
"Value": "6"
}, {
"Name": "Total_Friends",
"Value": "0"
}, {
"Name": "Device_Type",
"Value": "Phone"
}]
}
Try using $elemMatch
db.myCollection.aggregate([{$match: {"user_attribute": {$elemMatch: {"Name":"Quests_In_Progress", "Value":"0"}}}}, { $out, "temp"}])
That query will find anyone who has element of their array "Quests_In_Progress" with a value of 0 and put it into the collection temp
Here is an example of my data:
[
{
"_id": "54ff1f21592a15378825aa33",
"timeline": "54fb49274e3e0c17271205d9",
"name": "Ade Idowu",
"first_name": "Ade",
"last_name": "Idowu",
"cohort": {
"name": "Class III",
"color": "#308cea"
},
"__v": 3,
"tasks": [
{
"_id": "54ff2eb0a6299969a08f8797",
"personName": "Ade Idowu",
"projectName": "yec",
"startDate": "2015-03-19T23:00:00.000Z",
"endDate": "2015-03-06T23:00:00.000Z",
}
]
},
{
"_id": "54ff1f21592a15378825aa33",
"timeline": "54fb49274e3e0c17271205d9",
"name": "Bola Idowu",
"first_name": "Bola",
"last_name": "Idowu",
"cohort": {
"name": "Class III",
"color": "#308cea"
},
"__v": 3,
"tasks": []
}
]
I want to query person.tasks to display names of those whose tasks array is empty and those whose tasks.endDate has expired. I would appreciate using javascript or probably lodash.
Use lodash _.filter(collection, predicate):
Iterates over elements of collection, returning an array of all
elements predicate returns truthy for.
Predicate checks if iteree's task array is empty or the task endDate is smaller then the current time.
.getTime() returns the ms value of the date object so you can compare the two dates. This could be adjusted if you would like to filter by another granularity (ex. hours, days).
_.filter(dataObject, function(item){
var now = new Date();
return item.tasks.length == 0 || (newDate(item.tasks.endDate).getTime() < now.getTime());
});