JSON response from Watson Translator Undefined - javascript

I am trying to use Watson translator service from my node.js app through the API documentation from IBM https://www.ibm.com/watson/developercloud/alchemyvision/api/v1/#apiexplorer
var request = require("request");
var LanguageTranslatorV3 = require('watson-developer-cloud/language-translator/v3');
var english_message
var languageTranslator = new LanguageTranslatorV3({
version: '2018-05-01',
username: '1234',
password: '1234',
url: 'https://gateway.watsonplatform.net/language-translator/api'
});
function translatorEnglish(message) {
var parameters = {
text: message.text,
model_id: 'es-en'
};
languageTranslator.translate(
parameters,
function(error, response, body) {
if (error)
console.log(error)
else
console.log(JSON.stringify(response, null, 2));
}
);
}
I get the following correct response in logs
{
"translations": [
{
"translation": "Hi."
}
],
"word_count": 1,
"character_count": 4
}
but when i try to extract the output translation value i always get the output as Undefined.
console.log(response.translations.translation); => undefined
Can you please check and let me know if i am doing anything wrong?
Thanks

Try
console.log(response.translations[0].translation); // Hi
This is because translations is an array and has one item at index 0.
{
"translations": [
{
"translation": "Hi."
}
],
"word_count": 1,
"character_count": 4
}

Related

Working with javascript and API exasperates me: Always Response 400

I am trying to work with an API (that provides data about movies in the media centers of german public broadcasting) but I can't get a single useful response.
I got an example code in javascript but since I dont know anything about that language I try to understand as much as I can and write an equivalent python program. Can someone tell me my mistake? The code should return a list of movies along with their duration and some similar information.
The original goes like:
`
const query = {
queries: [
{
fields: ['title', 'topic'],
query: 'sturm der liebe'
},
{
fields: ['channel'],
query: 'ard'
}
],
sortBy: 'timestamp',
sortOrder: 'desc',
future: false,
offset: 0,
size: 10,
duration_min: 20,
duration_max: 100
};
const queryString = JSON.stringify(query); // I think this turns query into a json file
const request = new XMLHttpRequest(); // here I start to loose the understanding
const requestURL = 'https://mediathekviewweb.de/api/query';
request.open("POST", requestURL);
request.addEventListener('load', function (event) {
let response;
try {
response = JSON.parse(request.responseText);
} catch (e) { }
if (request.status == 200 && typeof response != 'undefined') {
for (let i = 0; i < response.result.results.length; i++) {
let entry = response.result.results[i];
let row = $('<tr>');
row.append($('<td>').text(entry.channel));
row.append($('<td>').text(entry.topic));
row.append($('<td>').text(entry.title));
row.append($('<td>').text(entry.description));
row.append($('<td>').text(entry.url_video));
$('#mediathek > tbody').append(row);
}
$('#responseText').text(JSON.stringify(response, null, 2));
} else {
if (response) {
console.log(response.err);
$('#errorText').html(response.err.join('</br>'));
}
else {
$('#errorText').html(request.statusText + '</br>' + request.responseText);
}
}
});
request.send(queryString);
`
I started using requests:
`
import requests
import json
url = "https://mediathekviewweb.de/api/query"
answer = requests.post(url, json=queryString)
print(answer)
print(json.loads(answer.content))
`
Already in this minimal example answer returns "<Response [400]>" and the content is "{'result': None, 'err': ['Unexpected token o in JSON at position 1']}"
Can someone help me out? I didn't give a json, where does this error come from?
I get the same response when I try
`
import requests
import json
url = "https://mediathekviewweb.de/api/query"
query = {
"queries": [], // I want to search through all movies so this is left clear
"sortBy": "timestamp",
"sortOrder": "desc",
"future": False,
"offset": 0,
"size": 1000,
"duration_min": 90,
"content-type": "text/plain"
}
queryString = json.dumps(query) # hopefully this is equivalent to JSON.stringify()
answer = requests.post(url, json=queryString)
print(answer)
print(json.loads(answer.content))
`
I actually think this extra code isn't even used by the program.
I don't know if thats the right direction, but I think the javascript programm first opens a request and then in an other step inputs the data. I can't do that with the requests package as far as I know.
Don't use XMLHttpRequest. This adds to the confusion.
Here is the fetch option:
const query = {
queries: [
{
fields: ['title', 'topic'],
query: 'sturm der liebe',
},
{
fields: ['channel'],
query: 'ard',
},
],
sortBy: 'timestamp',
sortOrder: 'desc',
future: false,
offset: 0,
size: 10,
duration_min: 20,
duration_max: 100,
};
const requestURL = 'https://mediathekviewweb.de/api/query';
fetch(requestURL, { method: 'POST', body: JSON.stringify(query) })
.then((res) => res.json())
.then((res) => {
// success request logic
console.log('response', res);
})
.catch((error) => {
// error handling
console.error(error);
});

parsing HashMap failed, expected Object, but encountered Array

For creating an action at hasura I'm using the following node.js code (still at an experimental stage) in glitch.com -
const execute = async (gql_query, variables) => {
const fetchResponse = await fetch(
"https://example.com/v1/graphql",
{
method: "POST",
body: JSON.stringify({
query: gql_query,
variables: variables
})
}
);
// console.log('DEBUG: ', fetchResponse);
const data = await fetchResponse.json();
console.log("DEBUG: ", data);
return data;
};
// paste the code from codegen here
const ACTION_INSERT_PAYSLIP_GET_DRIVER_PAYMENT_DATA = `
query getDriverPaymentData ($orders: [Int!]!) {
company_order (where: {company_order_id: {_in: $orders}}) {
company_order_details (distinct_on: stage_cost_driver_id) {
stage_cost_driver_id
company_user {
delivery_salary
}
}
}
}`
// Request Handler
app.post('/action_insert_payslip', async (req, res) => {
// get request input
const { order_list } = req.body.input
console.log('Input', order_list)
const orders = order_list.order_id
console.log('Item: ', orders)
const { data:driverPaymentData, errors:driverPaymentError} = await execute(ACTION_INSERT_PAYSLIP_GET_DRIVER_PAYMENT_DATA, orders)
console.log('Driver Payment Data: ', driverPaymentData)
// run some business logic
// success
return res.json({
// payslip_list: "<value>"
payslip_list: order_list
})
});
The query getDriverPaymentData produces an output like the following in hasura api explorer:
{
"data": {
"company_order": [
{
"company_order_details": [
{
"stage_cost_driver_id": 1,
"company_user": {
"delivery_salary": 20
}
},
{
"stage_cost_driver_id": 6,
"company_user": {
"delivery_salary": 10
}
}
]
},
{
"company_order_details": [
{
"stage_cost_driver_id": 6,
"company_user": {
"delivery_salary": 10
}
}
]
}
]
}
}
But in the log, I'm getting the following output:
Input { order_id: [ 247, 260, 253 ] }
Item: [ 247, 260, 253 ]
DEBUG: { errors:
[ { extensions: [Object],
message:
'parsing HashMap failed, expected Object, but encountered Array' } ] }
Driver Payment Data: undefined
It says that it expects object but encountered array. But from what I see, I'm already getting an object "data": {[....]} with array inside it from the output at hasura's API console.
What am I missing here? How can I get the data of stage_cost_driver_id and delivery_salary?
Shouldn't variables be an object?
body: JSON.stringify({
query: gql_query,
variables: {orders: variables}
})

Retrieve rows for multiple primary key values from AWS DynamoDB database

I am trying to say:
select * from myTable where pkName in ('john', 'fred', 'jane')
but there doesn't seem to be a native way to feed a list of items in an array. I have my query working and retrieving values for a single primary key but want to be able to pass in multiple ones. It seems this isn't possible from looking at the DynamoDb page in the console but is there a good workaround? Do I just have multiple OR in my KeyConditionExpression and a very complex ExpressionAttributeValues?
I'm referencing this page:
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html
And using code based on the following (which can be found at the address below):
var params = {
ExpressionAttributeValues: {
':s': {N: '2'},
':e' : {N: '09'},
':topic' : {S: 'PHRASE'}
},
KeyConditionExpression: 'Season = :s and Episode > :e',
ProjectionExpression: 'Title, Subtitle',
FilterExpression: 'contains (Subtitle, :topic)',
TableName: 'EPISODES_TABLE'
};
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/dynamodb-example-query-scan.html
You are looking for the batchGetItem function, documented here.
You can also use DocumentClient and batchGet.
const AWS = require('aws-sdk');
const dbClient = new AWS.DynamoDB.DocumentClient({ region: 'ap-south-1' });
exports.handler = (event, context, callback) => {
var cartItems=JSON.parse(event.body);
let scanningtable = {
RequestItems: {
COOLERS : {
Keys: [
{
"ITEM_ID": 379
},
{
"ITEM_ID": 376
}
],
ProjectionExpression: "ITEM_ID, #N,CATEGORY, SUB_CATEGORY, BRAND, SELL_RATE",
ExpressionAttributeNames: {
"#N": "NAME"
},
}
}
};
dbClient.batchGet(scanningtable, function (err, data) {
if (err) {
callback(err, null);
} else {
var response = {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*"
},
"body": JSON.stringify(data),
};
callback(null, response);
}
});
};

Join query with ElasticSearch

I need to do a join query on firebase using elasticsearch,
can anyone help me?
In particular I have two nodes, in the child node I have a field that is the id of the father node and I would like to have as a result all the fields of the father node.
How do I build my index in the code?
In adding, in my client I use elasticsearchclient,
here is an extract of the code to index a node:
var db = admin.database();
var etest = db.ref(type);
etest.on('child_added', createOrUpdateIndex);
etest.on('child_changed', createOrUpdateIndex);
etest.on('child_removed', removeIndex);
function createOrUpdateIndex(snap) {
client.index(index, type, snap.val(), snap.key)
.on('data', function(data) { console.log('indexed', snap.key + data ); })
.on('error', function(err) { console.log('index error ', err); }).exec();
}
function removeIndex(snap) {
client.deleteDocument(index, type, snap.key, function(error, data) {
if( error ) console.error('failed to delete', snap.key, error);
else console.log('deleted', snap.key);
});
}
And to take query results:
var queue = db.ref("search");
queue.child('request').on('child_added', processRequest);
function processRequest(snap) {
console.log('process...... ');
snap.ref.remove(); // clear the request after we receive it
var data = snap.val();
// Query ElasticSearch
client.search(index, data.type, { "query": { 'query_string': {
"query" : data.query
}}})
.on('data', function(results) {
var res = JSON.parse(results);
console.log(JSON.stringify(res.hits));
console.log("TOTAL " + JSON.stringify(res.hits.total));
queue.child('response/'+snap.key).set(results);
})
.on('error', function(error){ console.log("errore"); }).exec();
}
Thank you in advance
There are two ways to store your data.
First is creating a nested doc. This is helpful if you need to perform search on some of the values and need other info only for the display.
PUT /my_index {
"mappings": {
"yourIndex": {
"properties": {
"yourColumn": {
"type": "nested",
"properties": {
"name": { "type": "string" },
"parentNode": { "type": "string" },
"childNode": { "type": "string" }
}
}
}
}}}
Eg.
'str1', 'parent1', 'child1'
'str2' 'parent1', 'child2'
If you need not maintain any hierarchy,
you can simply create 3 separate columns and store like a flat data.
You can specify parent1 id and search for all the docs in both the cases.

create a PhoneCall record in Crm using Javascript

When I try to create a PhoneCall record in Crm using Javascript, I got following error message, I cannot figure out the reason, any help?
{
"readyState": 4,
"responseText": "{\r\n\"error\": {\r\n\"code\": \"\", \"message\": {\r\n\"lang\": \"en-US\", \"value\": \"Error processing request stream. The property name 'from' specified for type 'Microsoft.Crm.Sdk.Data.Services.PhoneCall' is not valid.\"\r\n}\r\n}\r\n}",
"status": 400,
"statusText": "Bad Request"
}
"error"
"Bad Request"
<code>
var fromArray = [];
var FromActivityParty = {
PartyId:
{
Id: Xrm.Page.context.getUserId(),
LogicalName: "systemuser"
},
ActivityId: {
Id: TeamId,
LogicalName: "team"
},
ParticipationTypeMask: { Value: 1 }
};
fromArray[0] = FromActivityParty;
var toArray = [];
var ToActivityParty = {
PartyId:
{
Id: Xrm.Page.data.entity.getId(),
LogicalName: "account"
},
ActivityId: {
Id: TeamId,
LogicalName: "team"
},
ParticipationTypeMask: { Value: 2 }
};
toArray[0] = ToActivityParty;
var PhoneCall = {
from: fromArray,
to: toArray,
Subject: "Create a phonecall record",
OwnerId: fromArray,
PhoneNumber: phoneNumber
}
CrmRestKit.Create("PhoneCall", PhoneCall)
.fail(function (xhr, status, errorThrown)
{
console.log(JSON.stringify(xhr, null, 4));
console.log(JSON.stringify(status, null, 4));
console.log(JSON.stringify(errorThrown, null, 4));
})
.done(function (data, status, xhr) {
console.log(JSON.stringify(data, null, 4));
}
</code>
If you're using the REST endpoint, you need to add the To and From using the relationship with the activity party entity.
Here is the code I use:
var phoneCall = {};
phoneCall.phonecall_activity_parties = [
new ActivityParty(2, "systemuser", "GUID"),
new ActivityParty(1, "contact", "GUID")
]; //2 = 'To' 1 = 'From'
//TODO: Call CREATE using phoneCall object.
function ActivityParty(typeMask, logicalName, partyId) {
if (partyId && partyId[0] === '{') {
partyId = partyId.substring(1, partyId.length - 1);
}
this.PartyId = {
LogicalName : logicalName,
Id : partyId
};
this.ParticipationTypeMask = {
Value : typeMask
};
}
Well the error message sounds pretty clear to me.
"from" seems not to be a valid property name for the phone call type.
A quick guess: Have you tried to use "From" with an upper case? I see that your other properties are written this way.

Categories