I have a script that outputs a json string via json_encode in PHP. The json string consists of
[{"custId":"2","custName":"John Inc"}]
The PHP script is initiated using
$.getJSON("customer.php", function(data){alert(data + ' ' + data.custName);});
The response is -
[object Object] undefined
Javascript recognises 'data' as an object but I cannot seem to reference the information using json dotted notation.
The data object is in an array so you need to access its elements keyed by an index:
alert(data[0].custName);
Also, I'd suggest installing firebug (assuming you are already using Firefox) and using console.log in lieu of alert. Its output is much more detailed and helpful.
Related
I want to search for particular texts inside the data received from an API call using the response.body function with the http library in Flutter. I have decoded the data with jsonDecode(data) into a var called decodedData.
How can i search for particular field inside this decodedData?
It's a map and you can use containsKey or containsValue to search for a particular text
Well if you just want to search for a particular string from whole body .then you can follow the steps Bellow
Convert the response body to a string using .tostring() method.
str1.contains('rem')
Use the .contains method to search for the dedired string
My webhook returns an array of elements. I need to make IBM Watson Assistant respond with those elements as options to the users.
How can I achieve that?
The JSON structure for an IBM Watson Assistant answer with options is documented. You already mentioned that your webhook returned an array of elements. It would need to match that structure.
Now, in your dialog you would need to add that options array myOptionsArray to your output. Assuming the array data is stored in the variable myvar, use something like this:
<? output.generic.addAll($myvar.myOptionsArray) ?>
The generic refers to the generic JSON output format - in contrast to the integrations JSON format. The above expression could be placed in a response or in some intermediate assignment. It might need some experimenting but works...
#data_henrik response was very helpful but it was little vague. So, I thought to post my own answer. I made some changes in my function so that the result it returns matches the options format just like in the image below.
And just as #data_henrik suggested, I stored that result in a context variable named myOptions and used it like :
<? output.generic.addAll($myOptions) ?>
I am using Databricks Resi API to create a job with notebook_task in an existing cluster and getting the job_id in return.
Then I am calling the run-now api to trigger the job.
In this step, I want to send a list as argument via the notebook_params, which throws an error saying "Expected non-array for field value".
Is there any way I can send a list as an argument to the job?
I have tried sending the list argument in base_params as well with same error.
user_json={
"name": job_name,
"existing_cluster_id": cluster_id,
"notebook_task": {
"notebook_path": notebook_path
},
"email_notifications":{
"on_failure":[email_id]
},
"max_retries": 0,
"timeout_seconds": 3600
}
response=requests.post('https://<databricks_uri>/2.0/jobs/create',headers=head,json=user_json,timeout=5, verify=False)
job_id=response.json()['job_id']
json_job={"job_id":job_id,"notebook_params":{"name":"john doe","my_list":my_list}}
response = requests.post('https://<databricks_uri>/2.0/jobs/run-now', headers=head, json=json_job, timeout=200, verify=False)
Not found any native solution yet, but my solution was to pass the list as a string and parse it back out on the other side:
json_job={"job_id":job_id,
"notebook_params":{
"name":"john doe",
"my_list":"spam,eggs"
}
}
Then in databricks:
my_list=dbutils.widgets.get("my_list")
my_list=my_list.split(",")
With appropriate care around special characters or e.g. conversion to numeric types.
If the objects in the list are more substantial, then sending them as a file to dbfs using the CLI or API before running the job may be another option to explore.
Hi may be I'm bit late but found a better solution.
Step 1:
Use JSON.stringyfy() in the console of any browser to convert your value(object, array, JSON etc) into string
Ex:
Now use this value in the body of URL
In Databricks notebook convert string to JSON using python json module.
Hope this helps
I'm trying to automate talend job executions using the Talend API but I'm getting an error when I try to pass the context params using the api.
The json I'm encoding to 64 is the following:
JSON='{ "actionName":"runTask", "authPass": "TalendPass", "authUser": "name#example.com", "jvmParams": [ "-Xmx256m" , "-Xms64m" ], "contextParams": ["host_mysql_db01": "failed", "database_analytics": "testing.it"],"mode": "synchronous", "taskId": 43}'
Error message:
{"error":"Expected a ',' or ']' at character 172","returnCode":2}
I found another stackoverflow issue Add context parameters to Talend job in Tac via API without actually running it but he doesn't say how he pass it and I cannot reply with a comment asking how he did it
The real talend api call is:
wget -O file http://localhost:8080/org.talend.administrator/metaServlet?$JSON_ENCODED
Can I get some help?
Actually, the json your are passing to the metaservlet is not valid json. You can check it with an online validator like http://jsonlint.com.
You are specifying the contextParams attribute as an array, but that syntax is not valid in json. An array can contain either a list of values (like jvmParams) or objects (which can themselves contain arrays). Here's an example.
Moreover, according to Talend reference, the attribute should be called "context" and must be an object instead of an array, like so:
"context":{"varname1": "varvalue", "varname2": "varvalue2"}
I'm using Restangular to connect with Mongolab. I'd like to use the following code to push an update:
var theData = {"$push":{exercises :{type: "running"}}};
Restangular.all('employees').one(user._id.$oid).customPUT(theData ,null, {apiKey: apiKey});
When I run this and look at the XHR request, the payload is always set to {}.
However, if I pull out the $, my payload looks like this:
{"push":{exercises :{type: "running"}}}
In that instance, the payload looks fine, but mongolab thinks I'm wanting to add a field named "push" instead of pushing to the excercises array since I'm not using the "$push" keyword.
I can have the "$" anywhere in the string except at the front (e.g " $push" and "push$" work) but unfortunately, that's what mongo requires in order to push an update. Is there some setting that I'm missing, or is this a bug in restangular?
yes, the $ will be striped: before the data is send, the data will be transformed with angular.toJson function:
#name angular.toJson
#function
#description
Serializes input into a JSON-formatted string. Properties with leading $ characters will be
stripped since angular uses this notation internally.
If you don't want this behavior you have to provide a transformRequest function (http://docs.angularjs.org/api/ng.$http). If your data is already json you may just write:
transformRequest: function(data){
return data;
}
the transformRequest must be provided as option during resource configuration. see
http://docs.angularjs.org/api/ngResource.$resource