Nodejs Customized JSON from lucid response - node.js

I have the following result set from the database, The result is an array of objects but I want it to look alike a nested objects as shown in the example.
[
{
"id": 4,
"entity": "dashboard",
"key": "message",
"value": "How to watch Netflix with XXX on Android?",
"locale": "en",
"created_at": "2022-10-25T14:41:38.000+05:00",
"updated_at": "2022-10-25T14:41:38.000+05:00"
},
{
"id": 9,
"entity": "in_survey",
"key": "content",
"value": "",
"locale": "en",
"created_at": "2022-10-25T14:41:38.000+05:00",
"updated_at": "2022-10-25T14:41:38.000+05:00"
}
]
Need a response like this from the above JSON
{
"dashboard": {
"message": "How to watch XYZ on Android?",
},
"in_survey": {
"content": ""
}
}

Related

Replacing 'media_keys' with 'preview_url' in Twitter API JSON return file?

I am doing an application in Node.js using the Twitter API. The way the Twitter API is setup you get a JSON file such as:
result = {
"data": [
{
"id": "1410481286947483648",
"created_at": "2021-07-01T06:11:58.000Z",
"text": "RT #weatherdak: Absolutely mind-blowing wildfire behavior in British Columbia.\n\nIncredible & massive storm-producing pyrocumulonimbus plumeā€¦"
},
{
"attachments": {
"media_keys": [
"7_1408149248726667266"
]
},
"id": "1408168068237434883",
"created_at": "2021-06-24T21:00:04.000Z",
"text": "RT #Brophyst: Tornado in Brno, Czech Republic :O "
},
{
"attachments": {
"media_keys": [
"16_1406870837026770949"
]
},
"id": "1406870843972632579",
"created_at": "2021-06-21T07:05:22.000Z",
"text": "Working on a new storm chasing application with incoming thunder rumbling. "
},
{
"id": "1405081317985947648",
"created_at": "2021-06-16T08:34:26.000Z",
"text": "#thefunkygirl #TornadoGreg #cgphotography Slender tornadoes are (almost) the best!"
}
],
"includes": {
"media": [
{
"media_key": "7_1408149248726667266",
"preview_image_url": "https://pbs.twimg.com/ext_tw_video_thumb/1408149248726667266/pu/img/qNKbx0MtaSGG0Sv0.jpg",
"type": "video"
},
{
"media_key": "16_1406870837026770949",
"preview_image_url": "https://pbs.twimg.com/tweet_video_thumb/E4Y1koXWUAU1Ls5.jpg",
"type": "animated_gif"
}
]
},
"meta": {
"oldest_id": "1405081317985947648",
"newest_id": "1410481286947483648",
"result_count": 10,
"next_token": "7140dibdnow9c7btw3z0ry2hiovfdas2sur0h2hu1m6ns"
}
}
How do I create a new JSON where instead of getting the attachments/media_keys subsection and the additional 'includes' subsection in the JSON, I can replace the attachment/media_keys with the preview_url?
For example, so that:
{
"attachments": {
"media_keys": [
"16_1406870837026770949"
]
},
"id": "1406870843972632579",
"created_at": "2021-06-21T07:05:22.000Z",
"text": "Working on a new storm chasing application with incoming thunder rumbling."
},
becomes:
{
"preview_image_url": "https://pbs.twimg.com/tweet_video_thumb/E4Y1koXWUAU1Ls5.jpg",
"type": "animated_gif"
"id": "1406870843972632579",
"created_at": "2021-06-21T07:05:22.000Z",
"text": "Working on a new storm chasing application with incoming thunder rumbling."
},

Azure API Set-body JSON to JSON covert

The response i am getting is below Which i need to convert the input JSON Format to other JSON structure and send the response back. I am struck how to get the data from the JSOn and construct the new JSON format
{
"totalSize": 1,
"done": true,
"records": [{
"attributes": {
"type": "test123",
"url": "/services/data/testapp"
},
"Id": "8373837",
"Name": "6294",
"Application": "9932932932",
"contact": {
"attributes": {
"type": "testcon",
"url": "/services/data/testappsss"
},
"Name": "testName",
"FirstName": "test",
"LastName": "name",
"MailingStreet": null,
"MailingCity": null,
"unemail": "testname#test,.co",
"MailingState": null,
"MailingCountry": null,
"MailingPostalCode": null,
"stuId": "328237832"
},
"currentusbss": "83277832873278",
"currentsu": {
"attributes": {
"type": "testsub",
"url": "/services/data/v44.0jsjsj"
},
"price": 2,
"Name": "SUB-20426"
},
"bal": 234,
"startdate": "2020-02-03",
"enddate": "2020-05-03"
}]
}
I need to convert above JSON format to below JSON format and send it using set-body method in out-bond policies
{
"info": {
"studentName": "testName",
"studentFirstName": "test",
"studentMiddleName": "",
"studentLastName": "Name",
"studentEmail": "testname#test,.co",
"role": "STUDENT",
"billingCountryCode": "US",
"systemId": "XX",
"stuId": "328237832"
},
"address": {
"address1": "1234 Grove St",
"address2": "",
"city": "Tempe",
"countryCode": "US",
"countryDescription": "UNITED STATES",
"stateCode": "AZ",
"stateDescription": "Arizona",
"postalCode": "45235",
"foreignState": "Arizona",
"region": "Domestic",
"phoneNumber": ""
},
"account": {
"institutionId": "1",
"paymentPlan": "N",
"currencyDesc": "United States Dollars",
"currencyType": "USD",
"bal": 234,
"daysLate":"18",
"opportunityId": "9932932932",
"studentParameterName": null,
"studentParameterValue": null
},
"studentTerms": [
{
"startdate": "2020-02-03",
"enddate": "2020-05-03",
"Name": "SUB-20426",
"description": "XQYember 03, 2020 "
}
]
}
You can use Liquid Template for this case:
Using Liquid Templates in Azure API Management
Using Liquid templates with set body
Or you create a new body in the outbound-section with a new JObject

MongoDB create product summary collection

Say I have a product collection like this:
{
"_id": "5a74784a8145fa1368905373",
"name": "This is my first product",
"description": "This is the description of my first product",
"category": "34/73/80",
"condition": "New",
"images": [
{
"length": 1000,
"width": 1000,
"src": "products/images/firstproduct_image1.jpg"
},
...
],
"attributes": [
{
"name": "Material",
"value": "Synthetic"
},
...
],
"variation": {
"attributes": [
{
"name": "Color",
"values": ["Black", "White"]
},
{
"name": "Size",
"values": ["S", "M", "L"]
}
]
}
}
and a variation collection like this:
{
"_id": "5a748766f5eef50e10bc98a8",
"name": "color:black,size:s",
"productID": "5a74784a8145fa1368905373",
"condition": "New",
"price": 1000,
"sale": null,
"image": [
{
"length": 1000,
"width": 1000,
"src": "products/images/firstvariation_image1.jpg"
}
],
"attributes": [
{
"name": "Color",
"value": "Black"
},
{
"name": "Size",
"value": "S"
}
]
}
I want to keep the documents separate and for the purpose of easy browsing, searching and faceted search implementation, I want to fetch all the data in a single query but I don't want to do join in my application code.
I know it's achievable using a third collection called summary that might look like this:
{
"_id": "5a74875fa1368905373",
"name": "This is my first product",
"category": "34/73/80",
"condition": "New",
"price": 1000,
"sale": null,
"description": "This is the description of my first product",
"images": [
{
"length": 1000,
"width": 1000,
"src": "products/images/firstproduct_image1.jpg"
},
...
],
"attributes": [
{
"name": "Material",
"value": "Synthetic"
},
...
],
"variations": [
{
"condition": "New",
"price": 1000,
"sale": null,
"image": [
{
"length": 1000,
"width": 1000,
"src": "products/images/firstvariation_image.jpg"
}
],
"attributes": [
"color=black",
"size=s"
]
},
...
]
}
problem is, I don't know how to keep the summary collection in sync with the product and variation collection. I know it can be done using mongo-connector but i'm not sure how to implement it.
please help me, I'm still a beginner programmer.
you don't actually need to maintain a summary collection, its redundant to store product and variation summary in another collection
instead of you can use an aggregate pipeline $lookup to outer join product and variation using productID
aggregate pipeline
db.products.aggregate(
[
{
$lookup : {
from : "variation",
localField : "_id",
foreignField : "productID",
as : "variations"
}
}
]
).pretty()

How to determine the type of the platform in Dialogflow (API.AI)

How do to identify from which platform the message came?
I want to support different platforms like Telegram and Facebook Messenger, When my webhook receive a message, I want to reply according to the platform the message came form.
For example, if the message came from Telegram I want to return a text message but if the message came from messenger I want to return a card.
You have a property source in originalRequest object, see fulfillment docs here.
{
"lang": "en",
"status": {
"errorType": "success",
"code": 200
},
"timestamp": "2017-02-09T16:06:01.908Z",
"sessionId": "1486656220806",
"result": {
"parameters": {
"city": "Rome",
"name": "Ana"
},
"contexts": [],
"resolvedQuery": "my name is Ana and I live in Rome",
"source": "agent",
"score": 1.0,
"speech": "",
"fulfillment": {
"messages": [
{
"speech": "Hi Ana! Nice to meet you!",
"type": 0
}
],
"speech": "Hi Ana! Nice to meet you!"
},
"actionIncomplete": false,
"action": "greetings",
"metadata": {
"intentId": "9f41ef7c-82fa-42a7-9a30-49a93e2c14d0",
"webhookForSlotFillingUsed": "false",
"intentName": "greetings",
"webhookUsed": "true"
}
},
"id": "ab30d214-f4bb-4cdd-ae36-31caac7a6693",
"originalRequest": {
"source": "google",
"data": {
"inputs": [
{
"raw_inputs": [
{
"query": "my name is Ana and I live in Rome",
"input_type": 2
}
],
"intent": "assistant.intent.action.TEXT",
"arguments": [
{
"text_value": "my name is Ana and I live in Rome",
"raw_text": "my name is Ana and I live in Rome",
"name": "text"
}
]
}
],
"user": {
"user_id": "PuQndWs1OMjUYwVJMYqwJv0/KT8satJHAUQGiGPDQ7A="
},
"conversation": {
"conversation_id": "1486656220806",
"type": 2,
"conversation_token": "[]"
}
}
} }

How to flatten JSON with nested structure before passing to #csv filter

I am trying to parse some JSON that is the output of an AWS CLI command to display Snapshots. I want to load this data up into a spreadsheet to be able to filter, group, and audit it.
I've been stumped on how to get the nested Tags array flattened into the parent objects such that the intermediate can then be passed to the #csv filter.
Here is the example:
Initial input JSON:
{
"Snapshots": [
{
"SnapshotId": "snap-fff",
"StartTime": "2014-04-01T06:00:13.000Z",
"VolumeId": "vol-fff",
"VolumeSize": 50,
"Description": "desc1",
"Tags": [
{
"Value": "/dev/sdf",
"Key": "device"
},
{
"Value": "a name",
"Key": "Name"
},
{
"Value": "Internal",
"Key": "Customer"
},
{
"Value": "Demo",
"Key": "Environment"
},
{
"Value": "Brand 1",
"Key": "Branding"
},
{
"Value": "i-fff",
"Key": "instance_id"
}
]
},
{
"SnapshotId": "snap-ccc",
"StartTime": "2014-07-01T05:59:14.000Z",
"VolumeId": "vol-ccc",
"VolumeSize": 8,
"Description": "B Desc",
"Tags": [
{
"Value": "/dev/sda1",
"Key": "device"
},
{
"Value": "External",
"Key": "Customer"
},
{
"Value": "Production",
"Key": "Environment"
},
{
"Value": "i-ccc",
"Key": "instance_id"
},
{
"Value": "B Brand",
"Key": "Branding"
},
{
"Value": "B Name",
"Key": "Name"
},
{
"Value": "AnotherValue",
"Key": "AnotherKey"
}
]
}
]
}
Desired Intermediate:
[
{
"SnapshotId": "snap-fff",
"StartTime": "2014-04-01T06:00:13.000Z",
"VolumeId": "vol-fff",
"VolumeSize": 50,
"Description": "desc1",
"device": "/dev/sdf",
"Name": "a name",
"Customer": "Internal",
"Environment": "Demo",
"Branding": "Brand 1",
"instance_id": "i-fff",
}
{
"SnapshotId": "snap-ccc",
"StartTime": "2014-07-01T05:59:14.000Z",
"VolumeId": "vol-ccc",
"VolumeSize": 8,
"Description": "B Desc",
"device": "/dev/sda1",
"Customer": "External",
"Environment": "Production",
"instance_id": "i-ccc",
"Branding": "B Brand",
"Name": "B Name",
"AnotherKey": "AnotherValue",
}
]
Final Output:
"SnapshotId","StartTime","VolumeId","VolumeSize","Description","device","Name","Customer","Environment","Branding","instance_id","AnotherKey"
"snap-fff","2014-04-01T06:00:13.000Z","vol-fff",50,"desc1","/dev/sdf","a name","Internal","Demo","Brand 1","i-fff",""
"snap-ccc","2014-07-01T05:59:14.000Z","vol-ccc",8,"B Desc","/dev/sda1","External","Production","i-ccc","B Brand","B Name","AnotherValue"
The following jq filter produces the requested intermediate output:
.Snapshots[] | (. + (.Tags|from_entries)) | del(.Tags)
Explanation: from_entries converts the array of key-value objects to an object with the given key-value pairs. This is added to the target object, and finally the "Tags" key is removed.
If the "target" object has a key that also appears in the "Tags" array, then the above filter will favor the value in the "Tags" array. You may accordingly wish to change the order of the operands of "+", or resolve the conflict in some other way.

Resources