Azure Logic App - Create Array with only one object - arrays

I am getting data from a powershell script from an automation account. i use this data to create reports and alerts. in 90% of the cases it is an json array.
So in my logic app i am going to parse the json and filter the array afterwards. i use the filtered data to create alerts based of the "id" in the filter. As i said in 90% of the cases this works perfectly.
But, if i only get one object from the script instead of the array i was getting the error:
"message": "Invalid type. Expected Array but got Object."
i solved this on the Parse Json step with:
"type": [
"object",
"array"
]
But now my "filter Array "is obviously not working anymore:
BadRequest. The 'from' property value in the 'query' action inputs is of type 'Object'. The value must be an array.
So i am trying to figure out how to solve this, is it possible to make an if/else statement and check if its an array or object?
Or should i initialize an empty array variable and append the object into the array so the filter will still work?

In the 'From' parameter of your 'Filter array' action, try using the array function to convert the object from your JSON into array.

Related

How to map array of objects

I have some problems mapping an array of objects in JS.
This is how the response looks in my console:
[{...}]
...and when I expand it I get this:
0:{id:0, document:{...}}
1:{id:1, document:{...}}
Usually the response I get is always without this number in front of each object, like this:
{id:0, document:{...}
{id:1, document:{...}
I tried every approach I know and I cant't manage to handle it.
The goal is to take each value out of "document" property and dynamically display it in some kind of table.
This is the way the browser devtools decides to display the array, but it is actually correct, if I'm not mistaken you're still dealing with an array.
You can verify this by logging the following:
(Replace myVar with the variable name you chose for your response array)
console.log(Array.isArray(myVar))
If it outputs true then you're fine and you are dealing with an array.

How to add an array to Firebase Firestore Swift

I am trying to update the data in a document in Cloud Firestore with an array however whenever I try to do this I get an error: Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Unsupported type: __SwiftValue (found in field upvotes)'. I would really appreciate it if someone could help me understand what I am doing wrong and how I can fix this. Thanks. Code is below.
Code for updating array-
db.collection("forum").document(data[0].id!).setData(["upvotes": data[0].upvotes!.remove(at: data[0].upvotes!.firstIndex(of: MainView.username ?? "Anonymous")!)])
You must pass an array to Firestore to update an array property. What you're doing is passing an element of an array. The remove(at:) method, when used like this:
let removedElement = someArray.remove(at: someIndex)
will remove the element but return the value of that removed element; it doesn't return the updated array. You must first remove the element and then get the truncated array:
someArray.remove(at: someIndex)
let updated = someArray
Therefore, first remove, then update:
data[0].upvotes!.remove(at: data[0].upvotes!.firstIndex(of: MainView.username ?? "Anonymous")! // first remove
db.collection("forum").document(data[0].id!).setData([
"upvotes": data[0].upvotes!) // then pass
])
However, I'd recommend reformatting the code because it doesn't read very well, IMO, and there is too much force unwrapping for my taste.

How to access object values using object["key"] approach

In the code I am referring, object attributes are accessed using the object["key"] method instead of calling object.key to access attribute values.
But when I try to create a simple object array and access attributes using above approach, I am getting below error.
if bank_record.effective_date.strip() == "25/07/2019" and bank_record["description"].__contains__("50036"):
TypeError: 'COM' object is not subscriptable
The reason given for object is not subscriptable error is missing __getitem__ method for the class. But in the code I am referring, it doesn't contain such method for any of the dto classes. But the above object["key"] method works just fine. What am I missing. I have been trying to figure this out for a while.
I just want to loop through a object array and access object attributes and modify them on the run. In order to make the function generic, I want to access these object attributes using object["key"] approach. Please help..
My mistake, I have missed set of steps. In the code I am referring, they are looping a json object array, which is created by dumping, python object array values into a json string and loaded back to a json object array.
excel_dto_list = []
#add objects to the list
#...
json_string = json.dumps([ob.__dict__ for ob in excel_dto_list])
#done in another method
downloaded_object = json.loads(json_string)
for x in downloaded_object:
print(x["comment"])

Laravel Eloquent - Working with an array of JSON objects

I have an array of JSON objects within my database (as depicted by the image below)
How do I search for a specific value within it? Say I want the record where the "ends_at" field is equal to "2018-11-24 08:00:00"
I've tried using
->where('column->starts_at', '2018-11-24 08:00:00')
or even tried
->whereJsonContains('column->starts_at', '2018-11-24 08:00:00').
Within the model I've cast the column to an array - am I missing something, or can this not be done?
Use this:
->whereJsonContains('column', ['starts_at' => '2018-11-24 08:00:00'])

XStream Single Element Array JSON

I'm generating some JSON with XStream for an object that contains an ArrayList. I have the #XStreamImplicit annotation set for the specific field. When I have 2 elements in the array it converts to JSON properly...
"cap": [
"switch",
"switch2"
]
However if there is only one element in the array I get this...
"cap": "switch"
Therefore it's not showing up as an array (even though there is only one element in it). This makes it a challenge to deserialize it on the other end (which I do not have control of). What I want it to do is the following...
"cap": [
"switch"
]
I'm assuming this is some sort of automatic assumption made for efficiency sake in the library. Either in XStream or Jettison. However is there a way to force the array setting here?

Resources