This is JSON 101 but I don't understand what i'm doing wrong.
This is for a discord.js bot, where a command has client passed to it. The client exists and provides all of the data that I would expect to see. The problem is that I then do not have access to the guilds or channel ids
Code:
module.exports = {
name: 'command',
execute(client) {
console.log(JSON.stringify(client))
console.log(JSON.stringify(client.guilds))
console.log(JSON.stringify(client.channels))
}
}
My response (reduced for "easier" reading):
{
"guilds": [
"00101010101010101"
],
"channels": [
"101010101010101010",
"101010011010101010"
]
}
{}
{}
Related
I am using chef inspec to validate in the below json file whether annotation is equal to Test.
"imdata": [
{
"aaaPwdStrengthProfile": {
"attributes": {
"annotation": "Test",
}
}
}
]
Trying with the below script but getting error
describe json('C:/output.json') do
its(['imdata','aaaPwdStrengthProfile','attributes','annotation']) { should eq 'Test' }
end
In the JSON file you are trying to test, imdata is an array []. The nested dictionary { .. } is the first element (0) of that array.
I created the JSON data from your question as /tmp/sample.json. So the inspec test should refer to 'imdata', 0,, like below:
describe json('/tmp/sample.json') do
its(['imdata', 0, 'aaaPwdStrengthProfile', 'attributes', 'annotation']) { should eq 'Test' }
end
I've been wondering for some days what kind of scheme would be more appropriate to use a data list in json in a web application.
I'm developing a REST Web Application, and im using Angular for front end, i should order, filter and print these data list also in xml ...
For you what scheme is better and why?
1) {
"datas": [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
]
}
2) {
"datas": [{
"data": { "first":"","second":""},
"data": { "first":"","second":""},
"data": { "first":"","second":""}
}]
}
3) [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
]
Thanks so much.
The first and third notations are quite similar because the third notation is included in your first.
So the question is "Should I return my datas as an array or should I return an object with a property that contain the array ?
It will depend on either you want to have more information alongside your datas or not.
For exemple, if your API might return an error, you will want to manage it from the front end.
In case of error, the JSON will looks like this :
{
"datas": null,
"error": "An error occured because of some reasons..."
}
At the opposite, if everything goes well and your API actually return the results, it will looks like this :
{
"datas": [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
],
"error": null
}
Then your front end can use the error property to manage errors sent from the API.
var result = getDatas(); // Load datas from the API
if(result.error){
// Handle the error, display a message to the user, ...
} else {
doSomething(result.datas); // Use your datas
}
If you don't need to have extra properties like error then you can stick with the third schema.
The second notation is invalid. The datas array will contain only one object which will have one property named data. In this case data is a property that is defined multiple times so the object in the array will contain only the last occurence:
var result = {
"datas": [{
"data": { "first":"a","second":"b"},
"data": { "first":"c","second":"d"},
"data": { "first":"e","second":"f"}
}]
}
console.log("Content of result.datas[0].data : ")
console.log(result.datas[0].data)
Obviously the first option would be easy to use. Once you will access datas it'll give you an array. Any operation (filter, sort, print) on that array will be easy in comparison to anything else. Everywhere you just need to pass datas not datas.data.
i am trying to aggregate two json payloads into one.
below json is stored in payload,
{
"clients":[
{
"status":"IN",
"clientSoftDeleteIndicator":"N",
"roles":[
{
"clientRoleCodeDesc":"PRIMARY OWNER",
"clientRoleCode":"PO",
"roleEffDate":null,
"clientRoleStatusCode":"IN",
"clientRoleStatusCodeDesc":"INACTIVE",
"roleSoftDeleteIndicator":"N",
"contractID":"49006649"
},
{
"clientRoleCodeDesc":"PRIMARY INSURED",
"clientRoleCode":"PI",
"roleEffDate":"2014-11-05",
"clientRoleStatusCode":"IN",
"clientRoleStatusCodeDesc":"INACTIVE",
"roleSoftDeleteIndicator":"N",
"contractID":"49006649"
},
{
"clientRoleCodeDesc":"PRIMARY INSURED",
"clientRoleCode":"PI",
"roleEffDate":"2014-11-05",
"clientRoleStatusCode":"IN",
"clientRoleStatusCodeDesc":"INACTIVE",
"roleSoftDeleteIndicator":"N",
"contractID":"49006648"
}
],
"clientID":72341935,
"statusDate":"2014-11-05",
"party":{
"tin4":"0346",
"addresses":[
{
"addrSoftDeleteIndicator":"N",
"city":"HOLLYWOOD",
"addressType":null,
"zip":"104620000",
"country":"U",
"line3":null,
"line2":null,
"line1":"5 MODEL OFFICE",
"county":null,
"state":"CA",
"validIndicator":null,
"preferredIndicator":null
}
],
"firstName":"SALLONARO",
"middleName":null,
"lastName":"PARETO",
"personSoftDeleteIndicator":"N",
"birthDate":"1987-11-10",
"sufName":null,
"preName":"MR",
"tin":"103380346",
"taxIdSoftDeleteIndicator":"N",
"gender":"M",
"phones":[
],
"martialStatus":null,
"emails":[
],
"tinType":"U"
},
"statusDesc":"INACTIVE"
}
]
}
and below json is stored in flowVariable contractVariable
{
"contracts":[
{
"policyEffDate":null,
"productCode":"97",
"policyStatusDate":"2014-11-05",
"cntrctSoftDeleteIndicator":"N",
"contractID":"49006649",
"adminSysCode":"FCT",
"dataSourceCode":null,
"policyStatus":"B3",
"lobCode":null,
"contractIssuerCode":null
},
{
"policyEffDate":null,
"productCode":"98",
"policyStatusDate":"2015-11-05",
"cntrctSoftDeleteIndicator":"N",
"contractID":"49006648",
"adminSysCode":"FCT",
"dataSourceCode":null,
"policyStatus":"B4",
"lobCode":null,
"contractIssuerCode":null
}
]
}
i want to copy the attributes from flowVariable, into payload when contractId matches.
below is my code snippet but all i am getting is empty json
%dw 1.0
%output application/json
---
payload.clients.roles map ((role,index) ->
{
contracts: ((flowVars.contractVariable.contracts default []) filter (role.contractID == $.contractID))
map ((contract ,indexOf) -> {
policyEffDate: contract.policyEffDate,
productCode: contract.productCode,
policyStatusDate: contract.policyStatusDate,
cntrctSoftDeleteIndicator: contract.cntrctSoftDeleteIndicator,
contractID: contract.contractID,
adminSysCode: contract.adminSysCode,
dataSourceCode: contract.dataSourceCode,
policyStatus: contract.policyStatus,
lobCode: contract.lobCode,
contractIssuerCode: contract.contractIssuerCode,
clientRoleCodeDesc: role.clientRoleCodeDesc,
clientRoleCode: role.clientRoleCode,
roleEffDate: role.roleEffDate,
clientRoleStatusCode: role.clientRoleStatusCode,
clientRoleStatusCodeDesc: role.clientRoleStatusCodeDesc,
roleSoftDeleteIndicator: role.roleSoftDeleteIndicator,
contractID: role.contractID
})
})
and the output i am getting is
[
{
"contracts": [
]
}
]
Can please someone help me on what is going wrong, i tried multiple ways but couldn't make it work.
I also went through Aggregate two payloads in Mule ESB which is similar to my requirement but it didn't work as well.
any help is highly appreciated.
i am sending an Array list from Producer and i am expecting to read the same arraylist at the consumer and persist into Database.
Instead of me getting the the Object i am getting and json wrapped inside the Object,which i am not able to understand why.
Below is representation of different objects.
Expexcting:
user is [Users [id=1, name=Prashantrh, nm=com.example.demo.Name#2b65d9e7]]
Pickied up at consumer side as:
[
[
{
"dmetaD":{
"id":2315,
"embedded":true,
"size":123,
"comment":"raghu",
"name":"string",
"type":"pdf",
"creationTime":"2018-05-15T20:47:48.161",
"creatorId":15001,
"creator":{
"id":15001,
"shortId":"MARC6GR",
"firstName":"V15001",
"lastName":"N15001",
"emailPref":true,
"departmentName":"RD/FNT",
"inventoryType":"P",
"langPref":"DE",
"email":"V15001.N15001#d.com"
}
},
"dCont":{
"data":"abc"
}
},
{
"dmetaD":{
"id":2316,
"embedded":true,
"size":123,
"comment":"raghu",
"name":"string",
"type":"pdf",
"creationTime":"2018-05-15T20:47:48.163",
"creatorId":15001,
"creator":{
"id":15001,
"shortId":"MARC6GR",
"firstName":"V15001",
"lastName":"N15001",
"emailPref":true,
"departmentName":"RD/FNT",
"inventoryType":"P",
"langPref":"DE",
"email":"V15001.N15001#d.com"
}
},
"dCont":{
"data":"def"
}
}
]
]
First, please provide more details as to what version of Spring Cloud Stream you are using.
That said, I am going to assume for now that you are using 2.0.0.RELEASE which means the content type of the message defaults to application/json.
I'm following this tutorial to push data from my API Gateway to a Kinesis stream :
http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-kinesis.html#api-gateway-get-and-add-records-to-stream
I have my Body Mapping Template setup as.....
{
"StreamName": "my-stream-name",
"Data": "$util.base64Encode($input.path('$.Data'))",
"PartitionKey": "$input.path('$.PartitionKey')"
}
...and have put the following in the Request Body of an API test...
{
"Data": {
"Foo": "A",
"Bar": "B"
},
"PartitionKey": "some key"
}
I've then created a Lambda Function which has a trigger set up against the same Kinesis Stream. However, I'm struggling to decode/deserialise the records coming in from Kinesis.
exports.handler = (event, context, callback) => {
event.Records.forEach(function(record) {
let payload = JSON.parse(Buffer(record.kinesis.data, 'base64').toString('ascii'))
});
};
It seems that the data is being serialised to Kinesis in a non-JSON format. The value for record.kinesis.data in the forEach loop is
e0Zvbz1BLCBCYXI9Qn0=
...which when push through Buffer(record.kinesis.data, 'base64').toString('ascii')
returns as
{Foo=A, Bar=B}
not
{"Foo":"A", "Bar":"B"}
Main aim is obviously to get payload to in a state where I can say console.log(payload.Foo)
Any hints as to what I should be doing/looking for would be appreciated.
For anyone else out there
I had my Body Mapping Template setup as.....
{
"StreamName": "my-stream-name",
"Data": "$util.base64Encode($input.path('$.Data'))",
"PartitionKey": "$input.path('$.PartitionKey')"
}
Now changed to handle the json....
{
"StreamName": "my-stream-name",
"Data": "$util.base64Encode($input.json('$.Data'))",
"PartitionKey": "$input.path('$.PartitionKey')"
}
where...
($input.json('$.Data'))
is the change :)