Problem with parsing results from a nested object - node.js

I have an object with the following structure, let's say the object name is apiCall:
{
"Status":"OK",
"ID":"xxx",
"Results":[ ]
}
And this is the structure of the Results array:
[
{
"Call":{
"ID":1,
"CustomerKey":null,
"CustomerID":null
},
"PingID":4,
"Key":null,
"Properties":[
{
"Name":"OrdinalID",
"Value":"1"
}
],
"CreatedDate":"0001-01-01T00:00:00.000",
"ID":0,
"ObjectID":null
}
]
Everything works perfectly fine and I am able to retrieve everything except the nested Properties.
So this is how I retrieve for example Call.ID when iterating through Results - this works fine:
apiCall.Results[i].Call.ID
Now when I try to get the Properties nothing works - I tried parsing, stringifying, and also the following:
apiCall.Results[i].Properties.Value;
apiCall.Results[i].Properties.OrdinalID;
Stringify(apiCall.Results[i].Properties.Value);
nothing from above works.
This is the only thing that somehow worked so far:
Stringify(apiCall.Results[i].Properties);
and it's giving the following result:
[{"Name":"OrdinalID","Value":"1"}]
How can I get to the actual value?
Thank you

Your Properties property is an array, so you need to include an index similar to what you did with Results[i].
Like so: apiCall.Results[i].Properties[0].Value (using 0 as the index here).

Related

how to store web activity result in a variable?

i have a web activity through which i am executing a rest API(execute queries)
this is a sample output of that:
{
"results": [
{
"tables": [
{
"rows": [
{
"[Value]": "2022-10-25T00:00:00Z"
}
]
}
]
}
]
i want to store the date value inside [Value] in a variable in adf
(variable value should be:2022-10-25T00:00:00Z)
but i am not able to do that because of square brackets
this is what i have tried
"#activity('SQl validation').output.results[0].tables[0].rows[0].[value]"
but it give me error
Position 73 Unrecognized expression: value
please suggest how i can fix this
Look at the following demonstration. I have the lookup which returns the following values.
To access the [companyId] attribute from this output array, I have used the following dynamic content (Using for loop just for demonstration):
#string(activity('myLookUp').output.value[item()]['[companyId]'])
So, you can use the following dynamic content instead:
#activity('SQl validation').output.results[0].tables[0].rows[0]['[value]']

How to get the key from a nested map (JSON) using given value in Groovy in JIRA Scriptrunner

Hope this question finds you all in good health.
As per title, would like to know how this is done in Groovy. Found a few, such as this, but the question and answer did not help.
The JSON is like this
def ​json = '''{
"boston": [
{
"name":"bob",
"phone":"242 123123",
},
{
"name":"alice",
"phone":"212-123-345",
}
],
"chicago": [
{
"name":"charlie",
"phone":"313-232-545",
},
{
"name":"denise",
"phone":"414-123-546",
}
]
}'''
But how do I use the value, for example bob to get boston?
When you use parsedjson['chicago']['email'], the result would be
[charlie#chicago.com, denise#chicago.com]
I tried to do something like
def getKey = parsedjson['email']?.key
as suggested here but in JIRA ScriptRunner console returned null
Any pointer is greatly appreciated in advance!
parsedjson['email']?.key returned null because key is not a List method. key is an Entry method so to find the key from a value you have to iterate through the Map's Entry Set.
Here's an example to get the city from the person's name using Map.find which returns an Entry:
parsedjson.find { it.value.find { it["name"] == "bob" } }.key

rally-node Add new item to collection

Using the rally-node toolkit, I'm getting an error message returned when attempting to add new Changeset, with new Changes inline, to an existing Userstory. When I have the new Changes commented out there's the Changeset is added as expected. It seems to be something specifically with the Change data that I can't figure out.
Add call:
rallyApi.add({
"ref":"https://rally1.rallydev.com/slm/webservice/v2.0/hierarchicalrequirement/91834286580",
"collection":"Changesets",
"data":[
{
"Author":{
"_ref":"https://rally1.rallydev.com/slm/webservice/v2.0/user/53634054872"
},
"CommitTimestamp":"2017-03-17T09:52:07-04:00",
"Message":"Feature/tags (#11)",
"Revision":"b8460460254cb79d3e72c98172c164f5c4d3493a",
"Uri":"https://ghe/org/repo/commit/b8460460254cb79d3e72c98172c164f5c4d3493a",
"Changes":[
{
"Action":"M",
"PathAndFilename":"file1.json",
"Uri":"https://ghe/org/repo/commit/b8460460254cb79d3e72c98172c164f5c4d3493a/file1.json"
},
{
"Action":"M",
"PathAndFilename":"file2.json",
"Uri":"https://ghe/org/repo/commit/b8460460254cb79d3e72c98172c164f5c4d3493a/file2.json"
}
],
"SCMRepository":{
"_ref":"https://rally1.rallydev.com/slm/webservice/v2.0/scmrepository/101417587520"
}
}
]
})
Returned error:
{ [Error: Could not create artifact to collection]
errors:
[ 'Could not create artifact to collection',
'Cannot parse object reference from "{null: {"Action": "M", "PathAndFilename": "file1.json", "Uri": "https://ghe/org/repo/commit/b8460460254cb79d3e72c98172c164f5c4d3493a/file1.json"}}"' ] }
It seems to be telling me it tried to find an existing change with that data, but I want to add a new change as specified in the docs and a recent question:
https://github.com/RallyTools/rally-node/wiki/User-Guide#add-to-a-collection
Rally API Add Tags to existing userstory NodeJS
I don't think you can do all of these things at the same time (although that would be pretty cool).
I'd first use the add method to add the changeset like you have specified, but without any changes. And then use the add method again to create the changes on that changeset. Does that work?

Ext.XTemplate loop through object

my controller returns data like this:
{
"success":true,
"data":{
"35":{
"msg":{
"32":{
"module_id":"35",
"alert_id":"32",
"alert_datetime":"2012-11-28 16:19:19",
"param1_type":"imo",
"param1_value":"453465",
"param2_type":"",
"param2_value":"0",
"param3_type":"",
"param3_value":"0",
"msg":"triiiis dve",
"count":1
},
"33":{
"module_id":"35",
"alert_id":"33",
"alert_datetime":"2012-10-28 00:00:00",
"param1_type":"imo",
"param1_value":"54984",
"param2_type":"",
"param2_value":"0",
"param3_type":"",
"param3_value":"0",
"msg":"triis tri",
"count":1
}
}
},
"42":{
"msg":{
"1":{
"module_id":"42",
"alert_id":"1",
"alert_datetime":"2012-10-28 16:19:19",
"param1_type":"imo",
"param1_value":"9281906",
"param2_type":"",
"param2_value":"0",
"param3_type":"",
"param3_value":"0",
"msg":"",
"count":1
}
}
},
"39":{
"msg":{
"2":{
"module_id":"39",
"alert_id":"2",
"alert_datetime":"2012-10-28 12:36:31",
"param1_type":"imo",
"param1_value":"65464546",
"param2_type":"",
"param2_value":"0",
"param3_type":"",
"param3_value":"0",
"msg":"",
"count":1
}
}
}
}
}
After that I do this
that.tpl.overwrite(that.el, Ext.decode(response).data);
The problem is that I can't loop through the result object keys... I know how to loop through objects with pre-defined key names, but mine are dynamicaly generated...
Will appreciate some help, thanks!
I am assuming you have an idea of the depth of nesting (4 levels below the "data" element in this case):
You could loop through the data with Ext.Object.each (maybe there are some query methods for this too, not sure), looping through each element's children too. In case you use Ext.data.Model instances, you can use the Ext.data.association links to loop through the data.
In that case you could make a different template for each level and insert the result of each template in the template of the level above.
It sounds more difficult than it actually is I think.
foreach in templates is currently indeed only available for support subscribers.

Cannot access properties on a populate()'d object from Mongoose?

This is very odd... I'm using populate() with a ref to fill in an array within my schema, but then the properties are inaccessible. In other words, the schema is like this:
new Model('User',{
'name': String,
'installations': [ {type: String, ref: 'Installations'} ],
'count': Number,
}
Of course, Insallations is another model.
Then I find & populate a set of users...
model.find({count: 0}).populate('installations').exec( function(e, d){
for(var k in d)
{
var user = d[k];
for(var i in user.installations)
{
console.log(user.installations[i]);
}
}
} );
So far so good! I see nice data printed out, like this:
{ runs: 49,
hardware: 'macbookpro10,1/x86_64',
mode: 'debug',
version: '0.1' }
However, if I try to actually ACCESS any of those properties, they're all undefined! For example, if I add another console log:
console.log(user.installations[i].mode);
Then I see "undefined" printed for this log.
If I try to operate on the object, like this:
Object.keys(user.installations[i]).forEach(function(key) { } );
Then I get a typical "[TypeError: Object.keys called on non-object]" error, indicating that user.installations[i] is not an object (even though it is outputted to the console as if it were). So, I even tried something ugly like...
var install = JSON.parse(JSON.stringify(user.installations[i]));
console.log(install, install.mode);
And, again, the first output (install) is a nice object containing the property 'mode'... but the 2nd output is undefined.
What gives?
Finally, I solved this...
I tried doing a console.log(typeof user.installations[i]); and got "string" as the output. This seemed odd, given that printing the object directly created console output (above) that looked like a normal object, not a string. So, I tried doing a JSON.parse(); on the object, but received the error "SyntaxError: Unexpected token r"
Finally, I realized what was going on. The "pretty console output" I described above was the result of a string formatted with \n (newlines). I had not expected that, for whatever reason. The JSON.parse() error is due to the fact that there is a known necessity with the node.js parser when attempting to parse object keys without quotations; see the SO question here:
Why does JSON.parse('{"key" : "value"}') do just fine but JSON.parse('{key : "value"}') doesn't? .
Specifically, note that the JSON parser in my case is failing on the character 'r', the fist character of "runs," which is the first key in my JSON string (above). At first I was afraid I needed to get a custom JSON parser, but then the problem hit me.
Look back to my original schema. I used a String-type to define the installation ref, because the array field was storing the installations's _id property as a String. I assume the .populate() field is converting the object to a String on output.
Finally, I looked at the Mongoose docs a little closer and realized I'm supposed to be referencing the objects based upon Schema.ObjectID. This explains everything, but certainly gives me some fixing to do in my schemas and code elsewhere...

Resources