aggregate nested object on next position of parent object - nested

I have a JSON object like this:
[
{
"questionText":"some text 1",
"answerText":"some text"
},
{
"questionText":"some text 2",
"answerText":"some text"
},
{
"questionText":"some text 3",
"answerText":"some text",
"disclosures":[
{
"questionText":"some text 4",
"answerText":"some text"
},
{
"questionText":"some text 5",
"answerText":"some text"
}
]
},
{
"questionText":"some text 6",
"answerText":"some text"
},
{
"questionText":"some text 7",
"answerText":"some text",
"disclosures":[
{
"questionText":"some text 8",
"answerText":"some text"
}
]
}
]
Where I must have to transform like this:
[
{
"questionText":"some text 1",
"answerText":"some text"
},
{
"questionText":"some text 2",
"answerText":"some text"
},
{
"questionText":"some text 3",
"answerText":"some text"
},
{
"questionText":"some text 4",
"answerText":"some text"
},
{
"questionText":"some text 5",
"answerText":"some text"
},
{
"questionText":"some text 6",
"answerText":"some text"
},
{
"questionText":"some text 7",
"answerText":"some text"
},
{
"questionText":"some text 8",
"answerText":"some text"
}
]
the keys of the nested object must be added on the next position of its parent. The values on its keys does not have numbers, so I cant ordenate then following the text.
How could I achieve this? Is it possible? Already tried to map like this but it adds the array at the end of the JSON output

For each element of the main array we can separate the keys that have array values, and pluck the array from it, and concatenate to the keys that don't have array values. A couple of flatten operations flattens the nested arrays. This solution assumes that the order of the arrays is always at the end of each item.
%dw 2.0
output application/json
---
flatten(payload flatMap (
[($ filterObject ((value, key, index) ->
!(value is Array)
))]
++
($ filterObject ((value, key, index) ->
value is Array
) pluck $)
))
Output:
[
{
"questionText": "some text 1",
"answerText": "some text"
},
{
"questionText": "some text 2",
"answerText": "some text"
},
{
"questionText": "some text 3",
"answerText": "some text"
},
{
"questionText": "some text 4",
"answerText": "some text"
},
{
"questionText": "some text 5",
"answerText": "some text"
},
{
"questionText": "some text 6",
"answerText": "some text"
},
{
"questionText": "some text 7",
"answerText": "some text"
},
{
"questionText": "some text 8",
"answerText": "some text"
}
]

If you want to try using Map you can try this approach and further you can order resulting payload based on questionText
%dw 2.0
output application/json
---
(flatten(payload map (
$.disclosures default []
)) ++ payload map (
($ - "disclosures")
))orderBy $.questionText
[
{
"questionText": "some text 1",
"answerText": "some text"
},
{
"questionText": "some text 2",
"answerText": "some text"
},
{
"questionText": "some text 3",
"answerText": "some text"
},
{
"questionText": "some text 4",
"answerText": "some text"
},
{
"questionText": "some text 5",
"answerText": "some text"
},
{
"questionText": "some text 6",
"answerText": "some text"
},
{
"questionText": "some text 7",
"answerText": "some text"
},
{
"questionText": "some text 8",
"answerText": "some text"
}
]

If you know that the internal arrays will come in disclosures fields only you can use flatmap directly to get a flat structure. Below script will keep the order in which you are getting the elements in the payload and is short and simple.
%dw 2.0
output application/json
---
payload flatMap (
if($.disclosures is Null) $
else ($ - "disclosures") >> $.disclosures //if there is a "disclosures" Array then move the items of this object at the top of disclosures Array and map to that
)

In this case I'd like to use Descendants to select the data (dw selectors). Grab all question and answer texts. Zip the two arrays so the results alternate and match up, flatMap and reduce the array items to create your desired object:
%dw 2.0
output application/json
import zip from dw::core::Arrays
var questions = payload..&questionText
var answers = payload..&answerText
---
(questions zip answers)
flatMap (QnA) ->
QnA reduce (item, acc={}) -> (
acc ++ item
)

Related

Docusign | Can we add custom field in to the document at specific position through API?

I am trying to add a custom field when I create an envelope and pass the one custom field. Is there any way to add a custom field in the document?
Is there any API Endpoint which we need to use?
Here's some helpful documentation for adding Custom fields to an envelope via the DocuSign API:
https://developers.docusign.com/docs/esign-rest-api/reference/envelopes/envelopecustomfields/
For example you can POST to this URL:
/restapi/v2.1/accounts/{accountId}/envelopes/{envelopeId}/custom_fields
And here's an example Body depending on if you want to use a text custom field or a list:
{
"textCustomFields": [
{
"fieldId": "sample string 1",
"name": "sample string 2",
"show": "sample string 3",
"required": "sample string 4",
"value": "sample string 5",
"configurationType": "sample string 6"
}
],
"listCustomFields": [
{
"listItems": [
"sample string 1"
],
"fieldId": "sample string 1",
"name": "sample string 2",
"show": "sample string 3",
"required": "sample string 4",
"value": "sample string 5",
"configurationType": "sample string 6"
}
]
}

How can I convert Xlsx / Xls file to JSON file (eg. sample.json) Node JS

I am trying to convert this excel sheet / file to json file as output not raw data (sample.json), I googled many resourses but I didn't find any working code or tutorials so I need some help, I have linked image that is excell data and how can I convert it into json file as below and upload it in MongoDB.
sample.json
{
"Sheet1": [
{
"Qusetion Number": "1",
"Question": "A program language",
"A": "defines the form of the instruction",
"B": "is always machine dependent",
"C": "is never machine dependent",
"D": "All of the above",
"E ": "None of the above",
"Answers": "defines the form of the instruction"
},
{
"Qusetion Number": "2",
"Question": "Quiestion 2",
"A": "Option A",
"B": "Option B",
"C": "Option C",
"D": "Option D",
"E ": "Option E ",
"Answers": "Option C"
},
{
"Qusetion Number": "3",
"Question": "Question 3",
"A": "Option 5",
"B": "Option 6",
"C": "Option 9",
"E ": "Option9",
"Answers": "Option 6"
},
{
"Qusetion Number": "4",
"Question": "Question 4",
"A": "456",
"B": "221",
"C": "889",
"Answers": "456"
}
]
}

How can I format a JSON with MongoDB to be use with Highcharts?

This is my end-point:
I have a JSON that look like this:
{
"_id": "5d30c48e5fb507250bb5",
"user": "5d2f594180064d82c47c",
"department_director": "Director 1",
"account_manager": "Account Manager 1",
"fronter": "Fronter 2",
"closer": "Closer 1",
"order_number": "EDVRV20202020202",
"business_name": "Business Name 3",
"business_website": "www.businessname3.com",
"client_name": "Client 3",
"client_phone": 9090909090,
"client_email": "email1#company.com",
"type_of_service": "Other",
"management_fee": 599, //I will use this objects
"sales_date": "2017-04-23T00:00:00.000Z",
"cancellation_date": "2018-04-23T00:00:00.000Z", // I will use this object,
"country": "USA",
"state": "Florida",
"cancellation_reason": "Business Closed",
},
And, I want to plot the following graph using type_of_service as a legend, cancellation_date as a x-axis and management_fee and a y_axi.

get values of select option in angular 4+

Hi I have a question how to get the values from list in a select option.
I have this array
{
"C_ID": "Item044",
"Descripcion": "Item 1",
"ID": "044"
},
{
"C_ID": "Item045",
"Descripcion": "Item 2 ,
"ID": "045"
},
{
"C_ID": "Item046",
"Descripcion": "Item 4",
"ID": "046"
},
{
"C_ID": "Item047",
"Descripcion": "Item 5",
"ID": "047"
},
and I get this values in a select option with NgFor but I need when selected for example Item 1 get the ID and C_ID in a input text automatic
and I get this values in a select option with NgFor but I need when selected for example Item 1 get the ID and C_ID in a input text automatic
Wire an ng model to the select input. And then wire the backed field to the input. Now when the select changes the input text should change.

How do I add a URL inside the field option in a Slack attachment?

I am trying to send notifications into the Slack channel from the external source. I am trying to put the URL link inside field tag of attachment. Is there a way to do that?
{
"attachments": [
{
"fallback": "Required plain-text summary of the attachment.",
"color": "#36a64f",
"pretext": "Optional text that appears above the attachment block",
"author_name": "Bobby Tables",
"author_link": "http://flickr.com/bobby/",
"author_icon": "http://flickr.com/icons/bobby.jpg",
"title": "Slack API Documentation",
"title_link": "https://api.slack.com/",
"text": "Optional text that appears within the attachment",
"fields": [
{
"title": "Priority",
"value": "High",
"short": false
}
],
"image_url": "http://my-website.com/path/to/image.jpg",
"thumb_url": "http://example.com/path/to/thumb.png"
}
]
}
Yes, you can use URLs in fields. But it only works in the value tag. There you can even use markup.
Here is an example in PHP:
$slack->addAttachment( array (
"fallback" => "Fallback",
"title" => "Title",
"text" => "text",
"fields" => array (
array (
"title" => "Priority",
"value" => "<http://i.imgur.com/nwo13SM.png|test>",
"short" => false
)
)
));

Resources