I want to convert my json list of data into an array
I tried using pluck and flatten but I got confused with the expression
I want to convert below json. Here relatedPartList and deliverableFileList are coming multiple times but I want to put it into an array.
{
"documentRevisionList": {
"entityId": "854598400000480352532DA50006EFDB",
"deleted": "false",
"revision": "A",
"revisionSequenceNumber": "0",
"latestRevision": "true",
"title": "PC FOR EBIZ TESTING",
"status": "Active",
"relatedPartList":
{
"tcpn": "145154-1",
"partRevision": "K",
"relationshipVerified": "true"
},
"relatedPartList": {
"tcpn": "271598-000",
"partRevision": "O",
"relationshipVerified": "true"
},
"relatedPartList": {
"tcpn": "825281-000",
"partRevision": "O",
"relationshipVerified": "true"
},
"deliverableFileList": {
"entityId": "854598400000480352532E3B000760FE",
"url": "https://dmtecdev.us.tycoelectronics.com/dmtec/documentDelivery/getFile?deliverableId=854598400000480352532E3B000760FE",
"fileName": "ENG_PC_10072013-2_A_PC.pdf",
"fileLanguage": "English",
"fileType": "pdf",
"fileSize": "13631",
"fileAttached": "true"
},
"deliverableFileList": {
"entityId": "000000000000F4E6561BFAB600055FA1",
"url": "https://dmtecdev.us.tycoelectronics.com/dmtec/documentDelivery/getFile?deliverableId=000000000000F4E6561BFAB600055FA1",
"fileName": "ENG_PC_10072013-2_A(830530).docx",
"fileLanguage": "English",
"fileType": "docx",
"fileSize": "12969",
"fileAttached": "true"
}
}
}
I want this type of response
"documentRevisionList": {
"entityId": "854598400000480352532DA50006EFDB",
"deleted": "false",
"revision": "A",
"revisionSequenceNumber": "0",
"latestRevision": "true",
"title": "PC FOR EBIZ TESTING",
"status": "Active",
"relatedPartList":
[
{
"tcpn": "145154-1",
"partRevision": "K",
"relationshipVerified": "true"
},
{
"tcpn": "271598-000",
"partRevision": "O",
"relationshipVerified": "true"
},
{
"tcpn": "825281-000",
"partRevision": "O",
"relationshipVerified": "true"
}
]
"deliverableFileList": [{
"entityId": "854598400000480352532E3B000760FE",
"url": "https://dmtecdev.us.tycoelectronics.com/dmtec/documentDelivery/getFile?deliverableId=854598400000480352532E3B000760FE",
"fileName": "ENG_PC_10072013-2_A_PC.pdf",
"fileLanguage": "English",
"fileType": "pdf",
"fileSize": "13631",
"fileAttached": "true"
},
{
"entityId": "000000000000F4E6561BFAB600055FA1",
"url": "https://dmtecdev.us.tycoelectronics.com/dmtec/documentDelivery/getFile?deliverableId=000000000000F4E6561BFAB600055FA1",
"fileName": "ENG_PC_10072013-2_A(830530).docx",
"fileLanguage": "English",
"fileType": "docx",
"fileSize": "12969",
"fileAttached": "true"
}]
}
This seems to be a simple case of using the * selector.
%dw 2.0
output application/json
---
{
documentRevisionList: {
entityId: payload.documentRevisionList.entityId,
deleted: payload.documentRevisionList.deleted,
...
relatedPartList: payload.documentRevisionList.*relatedPartList,
deliverableFileList: payload.documentRevisionList.*deliverableFileList
}
}
Bonus points: you could set a variable to payload.documentRevisionList for less verbose expressions.
Related
This seems like it should be straightforward but the fact it isn't suggests I haven't understood something.
I have a simple array variable with content as below. I want to filter it so I only see items where policy is X so that I can get the value for the document element.
I've tried:
filter #variables('myArray') where #equals('policy','X')
#contains('policy','X')
filter #variables('myArray') where #contains(#variables('myArray'),'X'
In each case the array enters the filter array action whole and leaves it completely empty. Any assistance gratefully received.
[
{
"document": "A",
"min": 7500001,
"policy": "X"
},
{
"document": "B",
"min": 7500001,
"policy": "Y"
},
{
"document": "C",
"min": 7500001,
"policy": "Z"
}
]
You can use Parse JSON before Filtering the array. Considering the sample which you have provided we have tested it in our Logic App and this is working. Here is the screenshot of my Logic App for your Reference:
RESULT:
Considering Another Sample of Array
[
{
"document": "A",
"min": 7500001,
"policy": "X"
},
{
"document": "B",
"min": 7500001,
"policy": "Y"
},
{
"document": "C",
"min": 7500001,
"policy": "Z"
},
{
"document": "D",
"min": 7500002,
"policy": "X"
}
]
RESULT:
Below is the code view of my logic app
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Filter_array_2": {
"inputs": {
"from": "#body('Parse_JSON')",
"where": "#equals(item()['policy'], 'X')"
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "Query"
},
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "SampleArray1",
"type": "array",
"value": [
{
"document": "A",
"min": 7500001,
"policy": "X"
},
{
"document": "B",
"min": 7500001,
"policy": "Y"
},
{
"document": "C",
"min": 7500001,
"policy": "Z"
}
]
}
]
},
"runAfter": {},
"type": "InitializeVariable"
},
"Parse_JSON": {
"inputs": {
"content": "#variables('SampleArray1')",
"schema": {
"items": {
"properties": {
"document": {
"type": "string"
},
"min": {
"type": "integer"
},
"policy": {
"type": "string"
}
},
"required": [
"document",
"min",
"policy"
],
"type": "object"
},
"type": "array"
}
},
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "ParseJson"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}
I have a mission to validate such a JSON message :
{
"header": {
"action": "change_time",
"taskGuid": "someTaskGuid",
"publishDate": "2012-04-23T18:25:43.511Z"
},
"data": {
"code": "f2103839",
"conditions": [
{
"conditionsType": "A",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
},
{
"conditionsType": "B",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
},
{
"conditionsType": "C",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
}
]
}
}
I've made such a JSON-schema to achieve that :
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "Some schema",
"description": "Some schema",
"type": "object",
"required": [
"header",
"data"
],
"properties": {
"header": {
"type": "object",
"required": [
"action",
"taskGuid",
"publishDate"
],
"properties": {
"action": {
"enum": [
"create_work_order",
"change_time",
"cancel_work"
]
},
"taskGuid": {
"type": "string"
},
"publishDate": {
"type": "string",
"format": "date-time"
}
}
},
"data": {
"type": "object",
"required": [
"code",
"conditions"
],
"properties": {
"code": {
"type": "string"
},
"conditions": {
"type": "array",
"items": [
{
"conditionsType": "object",
"properties": {
"type": {
"enum": [
"A",
"B",
"C"
]
},
"dateBegin": {
"type": "string",
"format": "date-time"
},
"dateEnd": {
"type": "string",
"format": "date-time"
}
},
"required": [
"conditionsType",
"dateBegin",
"dateEnd"
]
}
]
}
}
}
}
}
The conditions array will consist of 1-3 objects described by items. Each object should have a unique conditionsType.
I'm checking validation with this instrument - https://www.jsonschemavalidator.net/
The problem is that this schema does validate the message, But only the first object of array is processed as de. For instance, such a JSON is validated as well (see "conditions" object #2):
{
"header": {
"action": "change_time",
"taskGuid": "someTaskGuid",
"publishDate": "2012-04-23T18:25:43.511Z"
},
"data": {
"code": "f2103839",
"conditions": [
{
"conditionsType": "A",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
},
{
"conditionsType": 123,
"dateBegin": [1,2,3],
"dateEnd": 1
},
{
"conditionsType": "C",
"dateBegin": "2021-11-22T17:30:43.511Z",
"dateEnd": "2021-11-22T17:35:43.511Z"
}
]
}
}
Is that actually the right direction I've chosen for this task ?
Two things. You have a typo in your items schema where you actually want to have type and not conditionsType. Secondly, if the items keyword is an array, the items of the array are validated against the schemas in this order. You want to have the items keyword as a single schema which is then applied to all items. Your corrected schema for copy-paste:
{"$schema":"http://json-schema.org/draft-07/schema","title":"Some schema","description":"Some schema","type":"object","required":["header","data"],"properties":{"header":{"type":"object","required":["action","taskGuid","publishDate"],"properties":{"action":{"enum":["create_work_order","change_time","cancel_work"]},"taskGuid":{"type":"string"},"publishDate":{"type":"string","format":"date-time"}}},"data":{"type":"object","required":["code","conditions"],"properties":{"code":{"type":"string"},"conditions":{"type":"array","items":{"type":"object","properties":{"conditionsType":{"enum":["A","B","C"]},"dateBegin":{"type":"string","format":"date-time"},"dateEnd":{"type":"string","format":"date-time"}},"required":["conditionsType","dateBegin","dateEnd"]}}}}}}
I do have a JSON object shown below
[
{
"directories": [
{
"type": "folder",
"name": "Real Estate",
"order": "1",
"delete": "false",
"canModify": "true"
},
{
"type": "folder",
"name": "ABC",
"order": "8",
"delete": "false",
"canModify": "true"
},
{
"type": "folder",
"name": "Legal",
"order": "2",
"delete": "false",
"canModify": "false",
"directories": [
{
"type": "subfolder",
"name": "Lease",
"order": "9",
"delete": "false",
"canModify": "false"
}
]
},
{
"type": "folder",
"name": "Construction",
"order": "4",
"delete": "false",
"canModify": "true"
}
]
}
]
I have an array of object which i need to push inside the object having "name:Construction".
I Tried with following code
var folderObj = {
"name": "",
"canModify":'true',
"type":'folder',
"delete":'false',
"order": $scope.directories.length + 1
}
$scope.directories.push(folderObj);
The actual output what i need is to push the object inside a parent folder
[
{
"directories": [
{
"type": "folder",
"name": "Real Estate",
"order": "1",
"delete": "false",
"canModify": "true"
},
{
"type": "folder",
"name": "ABC",
"order": "8",
"delete": "false",
"canModify": "true"
},
{
"type": "folder",
"name": "Legal",
"order": "2",
"delete": "false",
"canModify": "false",
"directories": [
{
"type": "subfolder",
"name": "Lease",
"order": "9",
"delete": "false",
"canModify": "false"
}
]
},
{
"type": "folder",
"name": "Construction",
"order": "4",
"delete": "false",
"canModify": "true",
"directories": [
{
"type": "subfolder",
"name": "NewFolder",
"order": "10",
"delete": "false",
"canModify": "false"
}
]
}
]
}
]
As i am new to angular i was not able to figure it out how to push the object, So any help appreciated.
You need to write a recursive function. Try following
let arr = [{"directories":[{"type":"folder","name":"Real Estate","order":"1","delete":"false","canModify":"true"},{"type":"folder","name":"ABC","order":"8","delete":"false","canModify":"true"},{"type":"folder","name":"Legal","order":"2","delete":"false","canModify":"false","directories":[{"type":"subfolder","name":"Lease","order":"9","delete":"false","canModify":"false"}]},{"type":"folder","name":"Construction","order":"4","delete":"false","canModify":"true"}]}];
let obj = {"type":"subfolder","name":"NewFolder","order":"10","delete":"false","canModify":"false"};
let label = "Construction";
/* Function takes 3 inputs
* a - arr containing objects
* l - label - unique identifier of the match
* o - object that needs to be added */
function addObjectToTree(a, l, o) {
// Iterate over arr
for (let i = 0; i < a.length; i++) {
let p = a[i]; // get the object
if(p.name === l) { // if there is a match add to directories
p.directories = (p.directories || []).concat([o]);
return true;
} else if(p.directories) { // else check if there are sub-folders
// If there was a match in sub folders break the loop
if(addObjectToTree(p.directories, l, o)) break;
}
}
}
addObjectToTree(arr, label, obj);
console.log(arr);
I have 2 objects
{
"_id": "58b7f36b3354c24630f6f3b0",
"name": "refcode",
"caption": "Reference",
"type": "string",
"search": false,
"required": false,
"table": true,
"expansion": true
},
and
{
"_id": "58b7f36b3354c24630f6f3c8",
"vacancyid": "0",
"refcode": "THIS IS MY REF",
"position": "Test",
"jobtype": "Temp",
"department": "Industrial",
"branch": "Office",
"startdate": "02/12/2013",
"contactname": "Person Name",
"contactemail": "person#domain",
"Q_V_TYP": "Daily",
"score": 0
},
Object one defines what a field should be and what it is called
The second object is a job description.
What i need is to match a field to each key (this even sounds confusing i my head, so here is an example)
{
"_id": "58b7f36b3354c24630f6f3c8",
"vacancyid": "0",
"refcode": {
"_id": "58b7f36b3354c24630f6f3b0",
"name": "refcode",
"caption": "Reference",
"type": "string",
"search": false,
"required": false,
"table": true,
"expansion": true,
"value": "THIS IS MY REF"
}
},
"position": "Test",
"jobtype": "Temp",
"department": "Industrial",
"branch": "Office",
"startdate": "02/12/2013",
"contactname": "Person Name",
"contactemail": "person#domain",
"Q_V_TYP": "Daily",
"score": 0
},
Here you go:
var def = {
"_id": "58b7f36b3354c24630f6f3b0",
"name": "refcode",
"caption": "Reference",
"type": "string",
"search": false,
"required": false,
"table": true,
"expansion": true
};
var jobDesc = {
"_id": "58b7f36b3354c24630f6f3c8",
"vacancyid": "0",
"refcode": "THIS IS MY REF",
"position": "Test",
"jobtype": "Temp",
"department": "Industrial",
"branch": "Office",
"startdate": "02/12/2013",
"contactname": "Person Name",
"contactemail": "person#domain",
"Q_V_TYP": "Daily",
"score": 0
};
var jobDescKeysArr = Object.keys(jobDesc);
if (jobDescKeysArr.indexOf(def.name) !== -1) {
// A match.
def.value = jobDesc[def.name];
jobDesc[def.name] = Object.assign({}, def);
console.log(jobDesc)
}
I'm trying to loop through the results of a JSON response and get the title of each article, however my hosting company mentioned the returned object is not an array, so I can't loop through the articles.
Currently it gives me an undefined on the length of the amount of articles.
How can I loop through the articles and get their titles?
I tried everything below, and finally read somewhere I could use a for-in loop.
So I was checking out this example here: http://www.w3schools.com/js/tryit.asp?filename=tryjs_object_for_in
However, I have no idea how to apply this loop on my data to get just the articles and then loop through these and get their properties.
JSON RESPONSE
{
"blog": {
"id": "1464",
"comments": true,
"url": "blogs\/magazine",
"rss": "blogs\/magazine.rss",
"title": "Taj Magazine",
"articles": {
"3748": {
"id": 3748,
"image": 7442452,
"url": "blogs\/magazine\/foodies-walhalla-2",
"title": "Foodies Walhalla 2",
"author": "",
"author_url": false,
"summary": "dit is de samenvatting tekst",
"content": "dit is het hele artikel",
"date": "2013-11-13 13:54:00",
"comments": [],
"comments_count": 0,
"tags": {
"foodies-walhalla": {
"id": "9794",
"url": "blogs\/magazine\/tagged\/foodies-walhalla",
"title": "Foodies Walhalla",
"count": "2"
},
"magazine": {
"id": "9744",
"url": "blogs\/magazine\/tagged\/magazine",
"title": "magazine",
"count": "6"
}
}
},
"3747": {
"id": 3747,
"image": 7442441,
"url": "blogs\/magazine\/foodies-walhalla-1",
"title": "Foodies Walhalla 1",
"author": "",
"author_url": false,
"summary": "f df gdsfg",
"content": "dsfgsdfgdf sdfg",
"date": "2013-11-13 11:22:00",
"comments": [],
"comments_count": 0,
"tags": {
"foodies-walhalla": {
"id": "9794",
"url": "blogs\/magazine\/tagged\/foodies-walhalla",
"title": "Foodies Walhalla",
"count": "2"
},
"magazine": {
"id": "9744",
"url": "blogs\/magazine\/tagged\/magazine",
"title": "magazine",
"count": "6"
}
}
},
"3744": {
"id": 3744,
"image": 7442425,
"url": "blogs\/magazine\/beauty-artikel-2",
"title": "Beauty artikel 2",
"author": "",
"author_url": false,
"summary": "beauty article 2",
"content": "dfg sfg sdfg sdfgd gdsf df gsdf gdsf g",
"date": "2013-11-13 11:21:00",
"comments": [],
"comments_count": 0,
"tags": {
"beauty": {
"id": "9792",
"url": "blogs\/magazine\/tagged\/beauty",
"title": "beauty",
"count": "2"
},
"healthy": {
"id": "9745",
"url": "blogs\/magazine\/tagged\/healthy",
"title": "healthy",
"count": "2"
},
"magazine": {
"id": "9744",
"url": "blogs\/magazine\/tagged\/magazine",
"title": "magazine",
"count": "6"
}
}
},
"3745": {
"id": 3745,
"image": 7442417,
"url": "blogs\/magazine\/love-life-1",
"title": "Love & Life 1",
"author": "",
"author_url": false,
"summary": "dfgdsfgd",
"content": "f gdf gdfgdfg dfgdsfgdsfgdsfg",
"date": "2013-11-13 11:21:00",
"comments": [],
"comments_count": 0,
"tags": {
"love-life": {
"id": "9793",
"url": "blogs\/magazine\/tagged\/love-life",
"title": "love & life",
"count": "2"
},
"magazine": {
"id": "9744",
"url": "blogs\/magazine\/tagged\/magazine",
"title": "magazine",
"count": "6"
}
}
},
"3746": {
"id": 3746,
"image": 7442388,
"url": "blogs\/magazine\/love-life-2",
"title": "Love & Life 2",
"author": "",
"author_url": false,
"summary": "dfkjghksdjfh gdskfjdj sdlkjfhdjfghdjklfsdfkjhg df gd ghdkjsfhgkjsd fg lkjdshklgdfh kjds glkjsdh kljdsfh",
"content": "dfkjghksdjfh gdskfjdj sdlkjfhdjfghdjklfsdfkjhg df gd ghdkjsfhgkjsd fg lkjdshklgdfh kjds glkjsdh kljdsfh",
"date": "2013-11-13 11:21:00",
"comments": [],
"comments_count": 0,
"tags": {
"love-life": {
"id": "9793",
"url": "blogs\/magazine\/tagged\/love-life",
"title": "love & life",
"count": "2"
},
"magazine": {
"id": "9744",
"url": "blogs\/magazine\/tagged\/magazine",
"title": "magazine",
"count": "6"
}
}
}
}
},
"request": {
"url": "http:\/\/taj-ringen.webshopapp.com\/blogs\/magazine\/?format=json",
"method": "get",
"ssl": false,
"get": {
"format": "json"
},
"post": [],
"device": {
"platform": "windows",
"type": "webkit",
"mobile": false
}
},
"template": "pages\/blog.rain",
"renderer": "json"
}
JAVASCRIPT
<div id="loadstatus"></div>
<script type="text/javascript" language="javascript">
var newresult = '';
$.ajax({
type: "GET",
url: "http://taj-ringen.webshopapp.com/blogs/magazine/?format=json",
data: "",
dataType: "json",
success: function (mydata) {
console.log('load successful');
console.log('mydata.blog.articles.length: ' + mydata.blog.articles.length); //'undefined' error
for (var i = 0; i < mydata.blog.articles.length; i++) {
newresult += mydata.blog.articles[i].title;
}
}
});
</script>
I also had a look at this post: Convert Object to JSON string
tried the jQuery.parseJSON function
console.log(mydata);
var tmp = jQuery.parseJSON(mydata);
console.log(tmp);
console.log('tmp.blog.articles.length: ' + tmp.blog.articles.length);
But there I get an error Uncaught SyntaxError: Unexpected token o on line jquery-1.10.2.min.js:4
tried the $.map function
var array = $.map(mydata, function (e) {
return [$.map(e, function (v) {
return v;
})];
});
console.log(array);
console.log('tmp.blog.articles.length: ' + array.blog.articles.length);
Here I get Uncaught TypeError: Cannot use 'in' operator to search for '14' in pages/blog.rain on line jquery-1.10.2.min.js:4
What I'm trying to to is convert the object that is returned by the site and be able to loop through it.
Iterate through the article object, pulling out the keys associated with each article using the method described in the below post:
How to list the properties of a JavaScript object
You could also use the following loop:
for(var article in blog.articles) {
console.log(blog.articles[article].title);
}