Related
`I got this response from the api call and I need to extract specific values from the response. Also, I want to use it as a pre request script in another api call.
How can I extract values of the ids from the Json object?
2)Is it possible to extract particular value of the id key where key from equals "rahul.sharma#gmail.com"?
{
"content": [
{
"id": "e7ab9f7d-c9f4-47e3-8d53-6febcfb914",
"from": "raulsdirect#gmail.com",
"domainId": null,
"attachments": [],
"to": [
"af09331a-d681-48c4-9075-2c2965dc5ca34#mailslurp.mx"
],
"subject": "id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dc34221",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T15:43:02.357Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\">id 23543253534<div>sid 34645656452342342343424</div></div>\r\n",
"teamAccess": true,
"bodyMD5Hash": "F2956A8791EB5E6F6F6E259C112BB13B"
},
{
"id": "8d547247-32d2-4553-b1fe-33b4ca00221d2",
"from": "rahul.sharma#adtraction.com",
"domainId": null,
"attachments": [],
"to": [
"af09331a-d681-48c4-9075-243d965dc5ba8#mailslurp.mx"
],
"subject": "Re: saas",
"inboxId": "af09331a-d681-48c4-9075-2c263f2dc5ba8",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T22:20:23.301Z",
"read": true,
"bodyExcerpt": "<div dir=\"auto\"><div dir=\"auto\"></div><p style=\"font-size:12.8px\">sid 325sd-df435-3fdgvd435-gdfv43</",
"teamAccess": true,
"bodyMD5Hash": "948B78E301880858EB66ABDE6698450B"
},
{
"id": "446760be-e261-441a-bffe-fa31aa935239",
"from": "rahul.sharma#gmail.com",
"domainId": null,
"attachments": [],
"to": [
"10ea0b7b-b5eb-4c0f-908d-39437f2214a71#mailslurp.com"
],
"subject": "Complete your registration",
"inboxId": "10ea0b7b-b5eb-4c0f-908d-394354324a71",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T07:41:41.685Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "3A7619478AB69B1F63C99B9716896B1B"
},
{
"id": "79a2c183-5b72-4bc1-98aa-63bf5d52c2e6",
"from": "raulsdirect#gmail.com",
"domainId": null,
"attachments": [],
"to": [
"af09331a-d681-48c4-9075-2532165dc5ba8#mailslurp.mx"
],
"subject": "Re: id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dbdf5328",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T19:20:44.655Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "1ED8849F70CBCBBA6CF3CFEA0ACA66C4"
}
],
"pageable": {
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"last": true,
"totalElements": 4,
"totalPages": 1,
"size": 20,
"number": 0,
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"first": true,
"numberOfElements": 4,
"empty": false
}
`
To parse json, in JS there is a native way to do it.
JSON.parse()
Example of usage:
const data = JSON.parse(yourJsonObject);
then:
console.log(data.content) in your case will return:
[
{
"id": "e7ab9f7d-c9f4-47e3-8d53-6febcfb914",
"from": "raulsdirect#gmail.com",
"domainId": null,
"attachments": [],
"to": [
"af09331a-d681-48c4-9075-2c2965dc5ca34#mailslurp.mx"
],
"subject": "id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dc34221",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T15:43:02.357Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\">id 23543253534<div>sid 34645656452342342343424</div></div>\r\n",
"teamAccess": true,
"bodyMD5Hash": "F2956A8791EB5E6F6F6E259C112BB13B"
}, ...
]
as you want to find id of the content with from that equals to "rahul.sharma#gmail.com" and content is an array of object so, you can use filter method of array like this.
const result=response.content.filter(value=>value.from=="rahul.sharma#gmail.com");
result variable will contain the whole object which match with your email so if you want only id then you can get it by using another map property.
const ids=result.map(item=>item.id);
You can access to JSON object directly but if you want to convert to JS Object use JSON.parse()
//You can access to JSON object, but if you prefer to get JS Object do this: JSON.parse(objJSON)
const objJSON = {
"content": [
{
"id": "e7ab9f7d-c9f4-47e3-8d53-6febcfb914",
"from": "raulsdirect#gmail.com",
"domainId": null,
"attachments": [],
"to": [
"af09331a-d681-48c4-9075-2c2965dc5ca34#mailslurp.mx"
],
"subject": "id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dc34221",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T15:43:02.357Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\">id 23543253534<div>sid 34645656452342342343424</div></div>\r\n",
"teamAccess": true,
"bodyMD5Hash": "F2956A8791EB5E6F6F6E259C112BB13B"
},
{
"id": "8d547247-32d2-4553-b1fe-33b4ca00221d2",
"from": "rahul.sharma#adtraction.com",
"domainId": null,
"attachments": [],
"to": [
"af09331a-d681-48c4-9075-243d965dc5ba8#mailslurp.mx"
],
"subject": "Re: saas",
"inboxId": "af09331a-d681-48c4-9075-2c263f2dc5ba8",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T22:20:23.301Z",
"read": true,
"bodyExcerpt": "<div dir=\"auto\"><div dir=\"auto\"></div><p style=\"font-size:12.8px\">sid 325sd-df435-3fdgvd435-gdfv43</",
"teamAccess": true,
"bodyMD5Hash": "948B78E301880858EB66ABDE6698450B"
},
{
"id": "446760be-e261-441a-bffe-fa31aa935239",
"from": "rahul.sharma#gmail.com",
"domainId": null,
"attachments": [],
"to": [
"10ea0b7b-b5eb-4c0f-908d-39437f2214a71#mailslurp.com"
],
"subject": "Complete your registration",
"inboxId": "10ea0b7b-b5eb-4c0f-908d-394354324a71",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T07:41:41.685Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "3A7619478AB69B1F63C99B9716896B1B"
},
{
"id": "79a2c183-5b72-4bc1-98aa-63bf5d52c2e6",
"from": "raulsdirect#gmail.com",
"domainId": null,
"attachments": [],
"to": [
"af09331a-d681-48c4-9075-2532165dc5ba8#mailslurp.mx"
],
"subject": "Re: id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dbdf5328",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T19:20:44.655Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "1ED8849F70CBCBBA6CF3CFEA0ACA66C4"
}
],
"pageable": {
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"last": true,
"totalElements": 4,
"totalPages": 1,
"size": 20,
"number": 0,
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"first": true,
"numberOfElements": 4,
"empty": false
}
//Array of IDs of each content object
const arrayIDs = [];
objJSON.content.forEach( c => {
arrayIDs.push( c.id );
} )
//You can get the object filtering by the attribute you need
const rahulSharma = objJSON.content.find( c => {
return c.from == "rahul.sharma#gmail.com"
} )
//Then you can access to any attribute. In this case: id
console.log(`Sahul Sharma's ID: ${rahulSharma.id}`)
console.log('All IDs')
console.log(arrayIDs)
console.log('Info of Rahul Sharma')
console.log(rahulSharma)
I'm trying to return the uuid where the urns key has this match.
"urns": [
"whatsapp:27820111111"
],
What should be returned d7b9f88c-b359-4d69-926d-536c0d2892e0 since it has that phone number match. How can this be done programmatically.
Here's are the two dicts.
{
"next": "https://random.com=",
"previous": null,
"results": [
{
"uuid": "g2ws8dh5-b359-4d69-926d-536c0d2892e0",
"name": "James",
"language": "eng",
"urns": [
"whatsapp:27333333333"
],
"groups": [
{
"uuid": "57fs8430-3c78-42b7-876c-c385e36c1dba",
"name": "Postbirth WhatsApp"
}
],
"fields": {
"whatsapp_consent": null,
"webhook_failure_count": null,
},
"blocked": false,
"stopped": false,
"created_on": "2020-12-01T07:50:58.736502Z",
"modified_on": "2020-12-01T08:36:34.980359Z"
},
{
"uuid": "d7b9f88c-b359-4d69-926d-536c0d2892e0",
"name": "Jane",
"language": "eng",
"urns": [
"whatsapp:27820111111"
],
"groups": [
{
"uuid": "e35f2e39-3c78-42b7-876c-c385e36c1dba",
"name": "Prebirth WhatsApp"
}
],
"fields": {
"whatsapp_consent": null,
"webhook_failure_count": null,
},
"blocked": false,
"stopped": false,
"created_on": "2020-12-01T07:50:58.736502Z",
"modified_on": "2020-12-01T08:36:34.980359Z"
}
]
}
I'm able to loop through and find the urn but I'm not sure how to step back to get the uuid.
Pass in the results array and the urn string to this function. If found it will return the uuid, else it will return null.
function getIdFromUrn(results, urn) {
for (let i = 0; i < results.length; i++) {
const result = results[i];
if (result.urns.includes(urn)) {
return result.uuid; // urn found
}
}
return null; // not found
}
console.log(getIdFromUrn(results, 'whatsapp:27820111111'));
You don't need to loop when you can use find(...) to match against a value.
See demo below
var data = [{
"uuid": "g2ws8dh5-b359-4d69-926d-536c0d2892e0",
"name": "James",
"language": "eng",
"urns": [
"whatsapp:27333333333"
],
"groups": [{
"uuid": "57fs8430-3c78-42b7-876c-c385e36c1dba",
"name": "Postbirth WhatsApp"
}],
"fields": {
"whatsapp_consent": null,
"webhook_failure_count": null,
},
"blocked": false,
"stopped": false,
"created_on": "2020-12-01T07:50:58.736502Z",
"modified_on": "2020-12-01T08:36:34.980359Z"
},
{
"uuid": "d7b9f88c-b359-4d69-926d-536c0d2892e0",
"name": "Jane",
"language": "eng",
"urns": [
"whatsapp:27820111111"
],
"groups": [{
"uuid": "e35f2e39-3c78-42b7-876c-c385e36c1dba",
"name": "Prebirth WhatsApp"
}],
"fields": {
"whatsapp_consent": null,
"webhook_failure_count": null,
},
"blocked": false,
"stopped": false,
"created_on": "2020-12-01T07:50:58.736502Z",
"modified_on": "2020-12-01T08:36:34.980359Z"
}
];
const lookingFor = "whatsapp:27820111111";
const foundIt = data.find(el => {
return (el.urns.indexOf(lookingFor) !== -1);
});
console.log(foundIt.uuid);
{"activities": [
{
"actor": {
"id": 409,
"avatar": "",
"first_name": "Sakthi",
"last_name": "Vel",
"headline": null,
"is_online": false,
},
"foreign_id": "post.UsPost:253",
"id": "ed50e218-f3e8-11e8-8080-800132d8e9c0",
"object": {
"id": 253,
"comments": 0,
"likes": 0,
"files": [
{
"id": 112,
"file": "",
"content_type": "video/mp4",
"file_type": "video",
"created_at": "2018-11-29T15:10:38.524836Z"
}
],
"post_type": "post",
"is_bookmarked": false,
"is_liked": false,
"link": "/post/api/v1/253/",
"target": "post.UsPost:253",
"foreign_id": "post.UsPost:253",
"actor": {
"id": 409,
"avatar": "",
"first_name": "Sakthi",
"last_name": "Vel",
"headline": null,
"is_online": false,
},
"text": "#Multiple video (.mp4) test. Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.",
"skills": [
"VFX"
],
"created_at": "2018-11-29T15:10:37.426332Z",
"is_link": true,
"view_count": 0,
"created_by": 409
},
"origin": "user:409",
"target": "",
"time": "2018-11-29T15:10:37.426332",
"type": "post",
"verb": "posted"
}
{
"activities": [
{
"actor": {
"id": 64,
"last_name": "",
"headline": "Learning is my Passion",
"is_online": false,
"username": "Uchenna1"
},
"foreign_id": "post.UsPost:183",
"id": "447f6710-eb08-11e8-8080-8001369e78cd",
"object": {
"id": 183,
"comments": 1,
"likes": 1,
"files": [
{
"id": 87,
"content_type": "image/jpg",
"file_type": "image",
"created_at": "2018-11-18T08:02:18.759309Z"
}
],
"post_type": "post",
"is_bookmarked": false,
"is_liked": false,
"link": "/post/api/v1/183/",
"target": "post.UsPost:183",
"foreign_id": "post.UsPost:183",
"actor": {
"id": 64,
"first_name": "Uchenna",
"last_name": "",
"headline": "Learning is my Passion",
"is_online": false,
"username": "Uchenna1"
},
"text": "This is codigo",
"skills": [
"Javascript"
],
"created_at": "2018-11-18T08:02:17.626600Z",
"is_link": true,
"view_count": 10,
"created_by": 64
},
"origin": "user:64",
"target": "",
"time": "2018-11-18T08:02:17.626600",
"type": "post",
"verb": "posted"
}
],
"activity_count": 1,
"actor_count": 1,
"created_at": "2018-11-18T08:02:18.647108",
"group": "posted_2018-11-18",
"id": "451b1eab-eb08-11e8-8080-80007915e2b6.posted_2018-11-18",
"is_read": false,
"is_seen": true,
"updated_at": "2018-11-18T08:02:18.647108",
"verb": "posted"
}
I am facing issue in merging two object. Below is the snap of two object and result.
Here in the above image I wan to merge Old Data and new Data
but after merging getting wrong output i.e new updated data.
I am using command to do it.
let newData = { ...a , ...b }
console.log('new updated data: ',newData)
also I am performing this operation in redux action before dispatching.
Here {"activities": [....],....} is one object and I want to merge with the below one.
Can you clarify do your objects have a child that are objects or arrays and if you mean it's not merging those things or is it not merging anything at all?
for e.g:
let oldData = {
key: value,
key1: { childkey1: value1 }
}
if you have something like this and the problem is like I explained. It's because javascript doesn't do deep merge. it does a shallow merge.
Easiest option would be use a library like lodash that has a function for deep merge.
I have 2 JSON files:
A Template JSON
A JSON output (from awscli)
The template is a small JSON file as below:
{
"DryRun": true,
"ImageId": "",
"KeyName": "",
"SecurityGroups": [
""
],
"InstanceType": "",
"Monitoring": {
"Enabled": false
},
"SubnetId": "",
"DisableApiTermination": true,
"PrivateIpAddress": "",
"IamInstanceProfile": {
"Arn": "",
"Name": ""
},
"EbsOptimized": true,
"TagSpecifications": [{
"ResourceType": "",
"Tags": [{
"Key": "",
"Value": ""
}]
}]
}
The original file is the output of aws ec2 describe-instances:
{
"Reservations": [{
"OwnerId": "123456789012",
"ReservationId": "r-12345678",
"Groups": [],
"Instances": [{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com",
"RootDeviceType": "ebs",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": false,
"LaunchTime": "2016-02-09T03:06:21.000Z",
"PublicIpAddress": "12.34.56.78",
"PrivateIpAddress": "172.31.1.2",
"ProductCodes": [],
"VpcId": "vpc-1a2b3c4d",
"StateTransitionReason": "",
"InstanceId": "i-abcd1234",
"ImageId": "ami-1234abcd",
"PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal",
"KeyName": "tempKey",
"SecurityGroups": [{
"GroupName": "somegroup1",
"GroupId": "sg-ZZZZZ"
},
{
"GroupName": "somegroup2",
"GroupId": "sg-YYYYY"
}
],
"ClientToken": "NutKc123456789012",
"SubnetId": "subnet-00001234",
"InstanceType": "t2.medium",
"NetworkInterfaces": [{
"Status": "in-use",
"MacAddress": "02:AA:BB:CC:DD:EE",
"SourceDestCheck": true,
"VpcId": "vpc-1a2b3c4d",
"Description": "",
"Association": {
"PublicIp": "12.34.56.78",
"PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com",
"IpOwnerId": "123456789012"
},
"NetworkInterfaceId": "eni-XXXXXXXX",
"PrivateIpAddresses": [{
"PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal",
"Association": {
"PublicIp": "1.2.3.4",
"PublicDnsName": "ec2-12-34-56-78.ap-southeast-1.compute.amazonaws.com",
"IpOwnerId": "123456789012"
},
"Primary": true,
"PrivateIpAddress": "172.31.1.2"
}],
"PrivateDnsName": "ip-172-31-1-2.ap-southeast-1.compute.internal",
"Attachment": {
"Status": "attached",
"DeviceIndex": 0,
"DeleteOnTermination": true,
"AttachmentId": "eni-attach-XXXXXXXX",
"AttachTime": "2016-01-13T08:33:37.000Z"
},
"Groups": [{
"GroupName": "somegroup1",
"GroupId": "sg-ZZZZZZ"
},
{
"GroupName": "somegroup2",
"GroupId": "sg-YYYYYY"
}
],
"Ipv6Addresses": [],
"SubnetId": "subnet-00001234",
"OwnerId": "123456789012",
"PrivateIpAddress": "172.31.1.2"
}],
"SourceDestCheck": true,
"Placement": {
"Tenancy": "default",
"GroupName": "",
"AvailabilityZone": "ap-southeast-1b"
},
"Hypervisor": "xen",
"BlockDeviceMappings": [{
"DeviceName": "/dev/xvda",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vol-33221100",
"AttachTime": "2016-01-13T08:33:39.000Z"
}
}],
"Architecture": "x86_64",
"StateReason": {
"Message": "Client.UserInitiatedShutdown: User initiated shutdown",
"Code": "Client.UserInitiatedShutdown"
},
"RootDeviceName": "/dev/xvda",
"VirtualizationType": "hvm",
"Tags": [{
"Value": "SomeValue",
"Key": "SomeKey"
},
{
"Value": "AnotherValue",
"Key": "Name"
}
],
"AmiLaunchIndex": 0
}]
}]
}
I want to copy the values of the Keys in the original JSON file to the template file.
For example, KeyName is a common key between the 2 JSON files. The corresponding value tempKey is replaced in the template file.
The main use case of this is: I am trying to migrate a number of servers on AWS from 1 region to another. This is a part of migration process which will remove tons of manual clicking and configuration on AWS Console.
Note: I use BASH command line.
There's a way to do this with jq but it doesn't take a JSON template as input.
You'll have to modify it to become a query. This isn't the actual query you need, but something to get you started:
cat temp.json | jq '.Reservations[].Instances[] | { DryRun, ImageId, KeyName, SecurityGroups, InstanceType, Monitoring }'
Where temp.json is your output above that I placed into a file. For regular commands, just do something like aws ec2 describe-instances | jq ...
The output that gives me (keep in mind the restricted set I queried for) is:
{
"DryRun": null,
"ImageId": "ami-1234abcd",
"KeyName": "tempKey",
"SecurityGroups": [
{
"GroupName": "somegroup1",
"GroupId": "sg-ZZZZZ"
},
{
"GroupName": "somegroup2",
"GroupId": "sg-YYYYY"
}
],
"InstanceType": "t2.medium",
"Monitoring": {
"State": "disabled"
}
}
Hope this helps.
I am playing around with Freebase and have had some decent success, but have hit a wall. My MQL is below. I do not have any issue displaying name,latin name, etc, which I created in my base. I do not know how to display the article which is in a different base.
Here is the jQuery I am using to display data:
$('<div>',{text:this.name}).appendTo(document.body);
Thank you very much,
Todd
query : [
{
"/common/topic/article": {
"guid": null,
"limit": 1,
"optional": true
},
"/common/topic/image": {
"id": null,
"limit": 1,
"optional": true
},
"id": null,
"larval_food": [
{
"index": null,
"lang": "/lang/en",
"limit": 6,
"optional": true,
"sort": "index",
"type": "/type/text",
"value": null
}
],
"latin_name": [
{
"index": null,
"lang": "/lang/en",
"limit": 6,
"optional": true,
"sort": "index",
"type": "/type/text",
"value": null
}
],
"limit": 60,
"name": null,
"s0:type": [
{
"id": "/base/butterflies/butterfly",
"link": [
{
"timestamp": [
{
"optional": true,
"type": "/type/datetime",
"value": null
}
],
"type": "/type/link"
}
],
"type": "/type/type"
}
],
"sort": "-s0:type.link.timestamp.value",
"type": "/base/butterflies/butterfly"
}
]
Change
"type": "/base/butterflies/butterfly"
to the type of the thing you actually want to include.
As an aside, that looks like a query which was exported from one of the Freebase.com view pages. It can be greatly simplified and some of the stuff, like the sorting, you probably want removed altogether.
Here's your query simplied (I also recommend using the standard scientific name property instead of inventing your own "Latin name" property):
[{
"type": "/base/butterflies/butterfly",
"mid": null,
"name": null,
"/common/topic/article": [],
"/common/topic/image": ["mid":null,"optional":true],
"larval_food": [],
"latin_name": [],
"/biology/organism_classification/scientific_name" : [],
}]
Here's a version of the query which shows all organism classifications (species in this case) which have the tribe Danaini two levels up. It optionally decorates it with data (larval_food) from your base, if it exists:
[{
"type": "/biology/organism_classification",
"higher_classification": [{
"/biology/organism_classification/higher_classification": "Danaini"
}],
"mid": null,
"name": null,
"scientific_name": [],
"/common/topic/article": [],
"/common/topic/image": [{
"mid": null,
"optional": true
}],
"/base/butterflies/butterfly/larval_food": [],
}]
You can try it here: http://tinyurl.com/6wht7lx