I am trying to access an object (tmp_name) within a Woocommerce webhook payload in AWS Lambda. At the moment I can output the "line item" with:
exports.handler = (event, context, callback, err) => {
callback(null, event.line_items);
if (err) console.log('JSON Pass Fail'); // an error occurred
else console.log(event.order_key); // successful response
};
When I try to go deeper into the event data it runs as null, such as
callback(null,event.line_items.meta_data.value.tmp_name);
I have tried accessing the array with 0 and [0] with no joy either.
Eventually if I can access the file path, i need to strip out the backslahes so it looks like a proper file path. I have got the regex for that which is string.replace(/\\\//g, "/");
Can anyone help me put this together?
{
"line_items": [
{
"id": 2,
"name": "Audio 5",
"product_id": 15,
"variation_id": 0,
"quantity": 1,
"tax_class": "",
"subtotal": "10.00",
"subtotal_tax": "0.00",
"total": "10.00",
"total_tax": "0.00",
"taxes": [
],
"meta_data": [
{
"id": 20,
"key": "_wcj_product_input_fields_global_1",
"value": {
"name": "Aaron Ramsey's departure from Arsenal to Juventus.mp3",
"type": "audio\/mp3",
"tmp_name": "\/home\/site\/public_html\/wp-content\/uploads\/woocommerce_uploads\/wcj_uploads\/input_fields_uploads\/21.mp3",
"error": 0,
"size": 4943085,
"wcj_type": "file"
}
}
],
"sku": "",
"price": 10
}
],
"tax_lines": [
],
"shipping_lines": [
],
"fee_lines": [
],
"coupon_lines": [
],
"refunds": [
]
}
Related
I have this JSON response where I want to push "status": "pending", inside the nested Menu array, Please help how I can achieve this in Javascript.
[
{
"id": 1,
"status": "pending",
"menues": [
{
"title": "Coke",
"price": "200"
}
]
},
{
"id": 2,
"status": "delivered",
"menues": [
{
"title": "Pepsi",
"price": "120"
}
]
}
]
Here is what I want to achieve:
I just want to push the Staus key/value inside the Menu array
[
{
"id": 1,
"menues": [
{
"title": "Coke",
"price": "200",
"status": "pending",
}
]
},
{
"id": 2,
"menues": [
{
"title": "Pepsi",
"price": "120",
"status": "delivered",
}
]
}
]
You can go over the array, and for each item, go over the items in menues. Using the forEach method, this can even be done as a single statement:
arr = [
{
"id": 1,
"status": "pending",
"menues": [
{
"title": "Coke",
"price": "200"
}
]
},
{
"id": 2,
"status": "delivered",
"menues": [
{
"title": "Pepsi",
"price": "120"
}
]
}
];
arr.forEach(nested => {
nested.menues.forEach(menu => menu.status = nested.status);
delete nested.status
});
console.log(arr);
Maybe this is what you want? The following script creates a new nested array structure, leaving the original unchanged. Instead of deleting the status property from the outer object I limit the creation of properties in the newly created object to id and menues.
I changed this answer in response to OP's comment asking for ES5 and ES6 methods and the ... operator.
arr = [
{
"id": 1,
"status": "pending",
"menues": [
{
"title": "Coke",
"price": "200"
}
]
},
{
"id": 2,
"status": "delivered",
"menues": [
{
"title": "Pepsi",
"price": "120"
}
]
}
];
const res=arr.map(nested =>({ id:nested.id, menues:
nested.menues.map(menu =>({...menu,status:nested.status})) }));
console.log(res);
I have this knexnest query:
const query = knex.select([
'notes.id AS _id',
'notes.note AS _note',
'notes.timestamp AS _timestamp',
'customers.id AS _customerId',
'users.name AS _name',
'products.id AS _productId',
'tags.id AS _tags_tagId',
'tags.title AS _tags_tagTitle',
'tags.type AS _tags_tagType',
]).from('notes')
.join('users', 'notes.user_id', 'users.id')
.join('customers', 'notes.customer_id', 'customers.id')
.leftJoin('products', 'notes.product_id', 'products.id')
.leftJoin('note_tags', 'notes.id', 'note_tags.note_id')
.leftJoin('tags', 'note_tags.tag_id', 'tags.id')
.where('customers.id', customerId);
return knexnest(query);
My response json looks like this:
{
"id": 47,
"note": "This is an updated1 note",
"timestamp": "2019-07-12T15:17:27.281Z",
"customerId": 111781,
"name": "Paul",
"productId": 1,
"tags": {
"tagId": 4,
"tagTitle": "price",
"tagType": "product"
}
}
The problem is that the database returns more than one tag, only one is displayed. I'm expecting a response like this:
{
"id": 47,
"note": "This is an updated1 note",
"timestamp": "2019-07-12T15:17:27.281Z",
"customerId": 111781,
"name": "Paul",
"productId": 1,
"tags": {[
{
"tagId": 4,
"tagTitle": "price",
"tagType": "product"
},
{
"tagId": 5,
"tagTitle": "quality",
"tagType": "product"
}
]}
}
Have I got something wrong in my query that is causing this?
Got it. I was missing double __ in the tags:
'tags.id AS _tags__tagId',
'tags.title AS _tags__tagTitle',
'tags.type AS _tags__tagType'
I am new to JSON and Postman. I believe I'm trying to do something very simple.
I have created a GET request which will get a JSON response like the one below.
In the example below I want to get the count of All "IsArchived" attributes in the response;
The number of those attributes will vary from response to response.
How can I do it? Thanks in advance
{
"Id": 1328,
"Name": "AAA Test",
"Owner": {
"Id": 208,
"Name": "The Boss"
},
"FieldGroups": [
{
"Id": "c81376f0-6ac3-4028-8d61-76a0f815dbf8",
"Name": "General",
"FieldDefinitions": [
{
"Id": 1,
"DisplayName": "Product Name",
"IsArchived": false
},
{
"Id": 2,
"DisplayName": "Short Description",
"IsArchived": false
},
{
"Id": 33,
"DisplayName": "Long Description",
"IsArchived": false
},
]
},
{
"Id": "5ed8746b-0fa8-4022-8216-ad3af17db91f",
"Name": "Somethingelse",
"FieldDefinitions": [
{
"Id": 123,
"DisplayName": "Attribution",
"IsArchived": false
},
{
"Id": 1584,
"DisplayName": "FC1",
"IsArchived": false
},
{
"Id": 623,
"DisplayName": "Sizes",
"IsArchived": false,
"Owner": {
"Id": 208,
"Name": "The Boss"
},
"Unit": "",
"Options": [
{
"Id": 1,
"Value": "XS"
},
{
"Id": 2,
"Value": "S"
},
{
"Id": 3,
"Value": "M"
}
]
}
]
}
],
"IsArchived": false
"Version": 1
}
It is a rather specific solution but I hope it helps. The description is added as comments:
// Convert the response body to a JSON object
var jsonData = pm.response.json()
// Create a count variable which will be increased by 1 everytime IsArchived occurs
var count = 0;
function countIsArchived() {
// Loop through the FieldGroupsArray
_.each(jsonData.FieldGroups, (fieldGroupsArray) => {
// Loop through the FieldDefinitionsArray
_.each(fieldGroupsArray.FieldDefinitions, (fieldDefinitionsArray) => {
// Check if IsArchived exists
if(fieldDefinitionsArray.IsArchived) {
// Increase count by 1
count++;
}
});
});
// IF you want it:
// Check if IsArchived exists on the top level of the JSON response and increase count
if(jsonData.IsArchived) {
count++;
}
// IF you want it:
// Create a Postman environment variable and assign the value of count to it
pm.environment.set("count", count);
}
Additional info:
The , after the following object is not needed. It invalidates the JSON:
{
"Id": 33,
"DisplayName": "Long Description",
"IsArchived": false
}, <--
I need post data like array, but I send it always like this:
{
"books": [
{
"id": 19,
"name": "Král Lávra",
"author": "Karel Havlíček Borovský",
"yearOfRelease": "1920",
"price": "120",
"SubjectId": 1,
"UserId": 2,
"SchoolId": 1
},
]
}
I want show result like this:
[
{
"id": 19,
"name": "Král Lávra",
"author": "Karel Havlíček Borovský",
"yearOfRelease": "1920",
"price": "120",
"SubjectId": 1,
"UserId": 2,
"SchoolId": 1
},
]
I use sequelize to connection between database MySQL. This is my code for findAll books:
let books = await ctx.db.Book.findAll({
where: {
UserId: id
},
})
Then I only post to
ctx.body= {
books
}
Thanks for your response.
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.