Discussion:
work.fromJSON on JSON array put only first object in Entry
(too old to reply)
f***@gmail.com
2019-05-10 14:03:23 UTC
Permalink
Hi,

I tried the following in a script of an AL:

string='[{"userId":"TestA"},{"userId":"TestB"}]';
var json=work.fromJSON(string);
task.logmsg("json: "+json);

Result was:
json: {
"userId": "TestA"
}

Why is only the first object added to the Entry?

SDI Version is: 7.2.0.4 (Win 64 bit)
Please help!

Thx in advance and kind regards,
Frank
Jason Williams
2019-05-10 18:27:41 UTC
Permalink
Post by f***@gmail.com
Hi,
string='[{"userId":"TestA"},{"userId":"TestB"}]';
var json=work.fromJSON(string);
task.logmsg("json: "+json);
json: {
"userId": "TestA"
}
Why is only the first object added to the Entry?
SDI Version is: 7.2.0.4 (Win 64 bit)
Please help!
Thx in advance and kind regards,
Frank
We'll see what other say, though I suspect the method is expecting a string in the form of a json object instead of an array of json object. When I wrap your string in a json object the method took all the objects

string= '{"users": [{"userId":"TestA"},{"userId":"TestB"}]}'
var json=work.fromJSON(string);
task.logmsg("json: "+json);


json: {
"users": {
"users": {
"userId": "TestA"
},
"users": {
"userId": "TestB"
}
}
}
Eddie Hartman
2019-05-11 08:31:10 UTC
Permalink
Post by f***@gmail.com
Hi,
string='[{"userId":"TestA"},{"userId":"TestB"}]';
var json=work.fromJSON(string);
task.logmsg("json: "+json);
json: {
"userId": "TestA"
}
Why is only the first object added to the Entry?
SDI Version is: 7.2.0.4 (Win 64 bit)
Please help!
Thx in advance and kind regards,
Frank
As I often recommend, you should ask TDI how to solve something :)

Try this code to see how the system would deliver you the json you want:
---
e = system.newEntry();
e.addAttributeValue("att", "value1");
e.addAttributeValue("att", "value2");
task.logmsg(e);

json = e.toJSON();
task.logmsg("\n" + json);

e2 = e.fromJSON(json);
task.logmsg(e2);
---
There you'll see the you want to create an object with a property that holds an array of values:
{"att":["value1","value2"]}

Also note that to create JSON my preferred approach is to build the JS object and let TDI convert it for you:
---
jobj = {att: ["value1", "value2"]};
json = toJson(jobj);
entry = com.ibm.di.entry.Entry.fromJSON(json);
---

Hope this helps!

/Eddie
j***@gmail.com
2019-05-13 05:36:42 UTC
Permalink
An Entry in TDI is quite similar to a JSON object,
and it is not an array.
Therefore, when you use the work.fromJSON() method on something
that would be an array of objects, you will get only
the first object as an Entry.

If you want to process all the objects in the array, you must
write some code yourself where you do what you want with the
objects. Here is an example that will combine all the objects
into a combined entry with all the information, using the
TDI JSONParser to get all the entries:

string='[{"userId":"TestA"},{"userId":"TestB"}]';

var JSONParser = system.getParser("ibmdi.JSONParser")
JSONParser.setInputStream(string)
JSONParser.initParser()

var combined = system.newEntry();

while (e = JSONParser.readEntry()) {
task.logmsg("Read entry: " + e)
combined.merge(e, true)
}

task.logmsg("combined: " + combined)

Loading...