JSON Response IBM BPM - json

In our environment we have to prepare the response call with a JSON format (tw.local.jsonResponse). I'm new to JSON, not sure how to parse the date with the following JSON format in IBM BPM. It could be a great help if anyone guide/suggest (by using the following data).
Success Response
Code: 200
Content:
{
“status” : “success”
“data” : {
“change_number” : [string],
“instance” : [string],
“customer” : [string],
“tasks” : [
{ “task_number” : [string],
“description” : [string],
“schedule_start” : [datetime],
“schedule_end” : [datetime]
},
{ /* another task */ }
]
}
}

I have experienced similar problem with json and dates in IBM BPM.
In your solution, you can get the external JavaScript file json.js.
You can use the following function to return your JSON string:
BPMJSON.convertTWToJSON(tw.local.MyObject, false)

The first thing you need to do is make BO with require Datatype with the same name of BO.
Then use the following code to convert twObject to JSON:-
function createJson(twObject){
var jsonString="";
if(typeof twObject =='object' && twObject!=null){
if(twObject.listLength>0){
jsonString+="[";
for (var j=0; j<twObject.listLength; j++){
if(typeof twObject[j]!='string')
jsonString+="{";
for (var property in twObject[j].propertyNames){
var name = twObject[j].propertyNames[property];
if(typeof twObject[j][name]=='object'){
if(Object.prototype.toString.call(twObject[j][name]).indexOf("TWDate")!="-1"){
jsonString+="\""+name+"\":\""+twObject[j][name].format("yyyy-MM-dd'T'HH:mm:ss'Z'")+"\",";
}else{
jsonString+="\""+name+"\":"+createJson(twObject[j][name]);
}
}
else{
jsonString+="\""+name+"\":\""+twObject[j][name]+"\",";
}
if(twObject[j].listLength>0)
{
for (var k=0;k<twObject[j].listLength;k++){
jsonString+="\""+ twObject[j][k]+"\",";
}
}
}
if(typeof twObject[j] == 'string'){
jsonString+="\""+twObject[j]+"\"";
}
if(typeof twObject[j]!='string')
jsonString+="}";
if(j!=twObject.listLength-1){
jsonString+=",";
}
} jsonString+="],";
}else{
try{
if(twObject.propertyNames.length>0){
jsonString+="{";
for(var property in twObject.propertyNames){
var name = twObject.propertyNames[property];
if(typeof twObject[name]=='object'){
if(Object.prototype.toString.call(twObject[name]).indexOf("TWDate")!="-1"){
jsonString+="\""+name+"\":\""+twObject[name].format("yyyy-MM-dd'T'HH:mm:ss'Z'")+"\",";
}else{
jsonString+="\""+name+"\":"+createJson(twObject[name]);
}
}else{
jsonString+="\""+name+"\":\""+twObject[name]+"\",";
}
}
jsonString+="},"; }
else{
return "{},";
}}catch(e){
return "{},";
}
}
}
else if(typeof twObject =='object' && twObject==null){
return "{},";
}
return jsonString;
}
Source Code:http://www.ibpmcoding.com.

This is what worked in BPM 8.6 for a Rest Token:
tw.local.authToken = JSON.parse(tw.system.invokeREST(request).content).access_token;
Explained part by part:
tw.system.invokeREST(request)
Invoke the REST client to make a call.
tw.system.invokeREST(request).content
Contains the response as a jsonstring.
JSON.parse(tw.system.invokeREST(request).content).access_token
Parses it back to a json object and extract the attribute access_token as a string.

Humm.. I see why you have issue. Let me make it clear.
JSON Dates are not dates – they are Strings!!
So there is no fix format of date within JSON but we are lucky in case of IBM BPM.
String representation of date are in format of UTC time (indicated by the Z) in case of IBM BPM, for example
"2020-09-24T18:04:41.306Z"
You can convert this date string to JS Date on any browser using any date function for example
new Date("2020-09-24T18:04:41.306Z")
On server side JS you can use the same trick, converting sting date to JS date.

Related

How to prevent format change of date in view after save in ASP MVC

After saving DateTime in controller, I pass it back to the View but when it's displayed in view the value became /Date(1545062400000)/.
I already checked in the controller while in process if the data was changed but it did not since I'm just passing the viewmodel in the view.
[HttpPost]
public ActionResult UpdateHeader(RecordViewModel recordViewModel)
{
var ResultMessage = "";
if (ModelState.IsValid)
{
Record record = (from c in context.Records
where c.RecordId == recordViewModel.RecordId
select c).FirstOrDefault();
if (record == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
record.Description = recordViewModel.Description;
record.Date = recordViewModel.Date;
record.Remarks = recordViewModel.Remarks;
try
{
context.SaveChanges();
ResultMessage = "Record successfully updated.";
}
catch (Exception ex)
{
ModelState.AddModelError("", ErrorHelper.GetInnerException(ex));
ResultMessage = "Error: " + ErrorHelper.GetInnerException(ex);
}
}
var result = new { Model = recordViewModel, ResultMessage = ResultMessage };
return Json(result, JsonRequestBehavior.AllowGet);
}
Angular
self.submitEdit = function () {
var updateRecordHeader = RecordServices.updateRecordHeader(self.header)
.then(function successCallback(response) {
self.header = response.data.Model;
self.header.ResultMessage = response.data.ResultMessage;
}, function errorCallback(response) { toastr.error(response.statusText); });
}
The /Date(1545062400000)/ is known as date ticks using UNIX timestamp format since RFC7519 "epoch" (January 01, 1970), which cannot be directly consumed as JS Date object without converting it first. The reason behind usage of the ticks is JSON format doesn't have specific representation for DateTime struct when serialized to plain strings (see this reference).
You can create a custom function to convert ticks into JS Date object:
function toJSDate(value) {
var regex = /Date\(([^)]+)\)/;
var results = regex.exec(value);
var date = new Date(parseFloat(results[1])); // or parseInt
return date;
}
Or even simpler without regex by picking numeric values directly like this:
function toJSDate(value) {
var date = new Date(parseFloat(value.substring(6))); // or parseInt
return date;
}
Then use that function for ticks-to-date conversion:
// example
var header = response.data.Model;
var dateObject = toJSDate(header.Date);
// assign date object afterwards
Note that you may need to create another object structure which resembles response.data.Model but using server-side Date property with JS date object.
As an alternative you may create a getter-only string property which uses ToString() to convert DateTime value into desired string representation, then use it inside JS.
Side note:
Avoid using viewmodel property name which exactly matches built-in JS function names & objects (i.e. Date) for clarity.
Related issues:
How do I format a Microsoft JSON date?
Converting .NET DateTime to JSON

Possible to use regex with JSON and AJAX?

The Problem
Within an AJAX call, I am trying to retrieve a JSON object with an appended auto-generated identifier. Is it possible to use regex to select a JSON object that starts with a specific string?
E.g. below targeting announcements_414988813
jQuery
const parent = $('.c-banner');
let data;
$.getJSON('path_to_data.json', function (result) {
const data = result["jcr:content"]["parsys"];
const announcement = data["/^/announcements"];
let date = announcement.eventDate;
let _html = "";
_html += '<p>' + date + '</p>';
parent.append(_html);
});
JSON
{
"jcr:content": {
"parsys": {
"announcements_414988813": {
"eventDate": "Fri Jan 18 2019 00:00:00 GMT-1000",
"title": "Pizza Day!",
}
}
}
}
After you've extracted the data object, you should be able to loop through its keys and find the one that matches.
var announcement = {}
for (key in data) {
if (key.match(/^announcements/)) {
announcement = data[key];
}
}
Note that if your parsys object has multiple announcements, this would give you the last one. announcement will just remain an empty object if it doesn't find any. (Break out of the loop if you want the first one, or save them all in an array if you need them all.)

Taking out a text from a String as a Key from Json

I need to take out 1000 from this Key-Value Pair in node Js
"msg":"amamm : ErrorResponse {\"abcd1\":{\"Status\":\"E\",\"rem\":{\"Code\":\"1000\",\"message\":\"Unable to access your information at this time.(1000)\"}}}"
The JSON what you have posted is invalid. May be your JSON should be like the following:
var json = {
"msg": {
"amm": {
"ErrorResponse": {
"abcd1": {
"Status": "E",
"rem": {
"Code": "1000",
"message": "Unabletoaccessyourinformationatthistime.(1000)"
}
}
}
}
}
}
Then in node.js you have get the value using dot notation like as follows:
json.msg.amm.ErrorResponse.abcd1.rem.code
Will give you the value 1000
Assuming you can get the string
amamm : ErrorResponse {\"abcd1\":{\"Status\":\"E\",\"rem\":{\"Code\":\"1000\",\"message\":\"Unable to access your information at this time.(1000)\"}}}
by doing err.msg
You can get the json and then get the string you want. I copied the content of the msg into a variable str just to test the regex
var re = /\{.*?\}$/;
var str = 'amamm : ErrorResponse {\"abcd1\":{\"Status\":\"E\",\"rem\":{\"Code\":\"1000\",\"message\":\"Unable to access your information at this time.(1000)\"}}}';
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
var content = JSON.parse(m[0]);
console.log(content);
console.log(content.abcd1.rem.Code);
}
Here's a working example: http://jsfiddle.net/sger1gk6/

typeof comparison NOT equal to fails (JAVASCRIPT)

I'm trying to convert any item within a JSON object to a string. JSON.stringify won't work because it doesn't convert the individual values. If its an object or number, I want the entire object to be a string. How do I test if typeof is NOT a string. I can't figure out why this doesn't work...
if (typeof(value) !== 'string') {
return String(value);
}
Any insights? Full example below:
var myjson = {
"current_state":"OPEN",
"details":"Apdex < .80 for at least 10 min",
"severity":"WARN",
"incident_api_url":"https://alerts.newrelic.com/api/explore/applications/incidents/1234",
"incident_url":"https://alerts.newrelic.com/accounts/99999999999/incidents/1234",
"owner":"user name",
"policy_url":"https://alerts.newrelic.com/accounts/99999999999/policies/456",
"runbook_url":"https://localhost/runbook",
"policy_name":"APM Apdex policy",
"condition_id":987654,
"condition_name":"My APM Apdex condition name",
"event_type":"INCIDENT",
"incident_id":1234
};
function replacer(key, value) {
if (typeof(value) !== 'string') {
return String(value);
}
return value;
}
console.log(JSON.stringify(myjson, replacer));
This actually isn't a problem with the typeof comparison.
The replacer function is initially called with an empty key and a value representing the entire JSON object (reference). Since the JSON object is not a string, the first thing your replacer function does is replace the whole JSON object with the string "[object Object]".
To fix this, check that the key does, in fact exist. Thus, your replacer function will look like this:
function replacer(key, value) {
if (key && (typeof(value) !== 'string')) {
return String(value);
}
return value;
}
I have a working fiddle of it here as well.

How do I pull out the JSON field I want using Jackson TreeNode and JsonNode?

I'm a little stumped why I can't pull the "Type" field out of my JSON stream to make a decision. It seems like this should be so easy.
I have the following JSON that I have as input:
[
{
"Institution":"ABC",
"Facility":"XYZ",
"Make":"Sunrise",
"Model":"Admission",
"SerialNumber":"",
"Revision":"1",
"Type":"ABC_Admission",
"ArchiveData":"<CSV file contents>"
}
]
In my Java I have a try-catch block with a JsonHolder class that implements Serializable to hold the JSON. Here's the Java I currently have:
try {
// Parse and split the input
JsonHolder data = JsonHolder.getField("text", input);
DataExtractor.LOG.info("JsonHolder data= " + data);
TreeNode node = data.getTreeNode();
DataExtractor.LOG.info("node size= " + node.size());
node = node.path("Type");
JsonNode json = (JsonNode) node;
DataExtractor.LOG.info("json= " + json.asText());
// code to decide what to do based on Type found
if (json.asText().equals("ABC_Admission")) {
// do one thing
} else {
// do something else
}
} catch (IOException iox) {
DataExtractor.LOG.error("Error extracting data", iox);
this.collector.fail(input);
}
When I run my code I get the following output (NOTE: I changed my package name where the class is to just for this output display)
25741 [Thread-91-DataExtractor] INFO <proprietary package name>.DataExtractor - JsonHolder data= [
{
"Institution":"ABC",
"Facility":"XYZ",
"Make":"Sunrise",
"Model":"Admission",
"SerialNumber":"",
"Revision":"1",
"Type":"ABC_Admission",
"ArchiveData":"<CSV file contents>"
}
]
25741 [Thread-91-DataExtractor] INFO <proprietary package name>.DataExtractor - node size= 1
25741 [Thread-91-DataExtractor] INFO <proprietary package name>.DataExtractor - json=
As you can see I don't get anything out. I just want to extract the value of the field "Type", so I was expecting to get the value "ABC_Admission" in this case. I would have thought the node path would separate out just that field from the rest of the JSON tree.
What am I doing wrong?
After consulting with another developer I found out the issue is my JSON is inside an array. So, I need to iterate over that array and then pull out the Type field from the object.
The updated code to resolve this is below:
try {
// Parse and split the input
JsonHolder data = JsonHolder.getField("text", input);
DataExtractor.LOG.info("JsonHolder data= " + data);
TreeNode node = data.getTreeNode();
String type = null;
// if this is an array of objects, iterate through the array
// to get the object, and reference the field we want
if (node.isArray()){
ArrayNode ary = (ArrayNode) node;
for (int i = 0; i < ary.size(); ++i) {
JsonNode obj = ary.get(i);
if (obj.has("Type")) {
type = obj.path("Type").asText();
break;
}
}
}
if (type == null) {
// Do something with failure??
}
DataExtractor.LOG.info("json= " + type);
if (type.equals("ABC_Admission")) {
// do one thing
else {
// do something else
}
} catch (IOException iox) {
DataExtractor.LOG.error("Error extracting data", iox);
this.collector.fail(input);
}