Skip to content
How to add an item ...
 
Notifications
Clear all

How to add an item to a collection

1 Posts
1 Users
0 Reactions
6 Views
json_onoDK
(@json_onodk)
Posts: 1
New Member
Topic starter
 

Script to call an API and check for an object

The goal of this script is to call an API endpoint that provides multiple parameters. One of these parameters is a feature mapping which has two parts. I want to verify if this feature is already mapped in the list; if not, I need to add it.

I've experimented with several approaches but haven't found a logic that successfully adds items to the end.

Attempt One: Altering the string itself.

Initially, I attempted to use the JSON string received from the API call for indexOf(jsonString; {"id1":"value1","id2":"value2"}) but this did not yield the desired results. Since value1 and value2 can be found partially within the JSON string, I cannot even use two conditions where one is indexOf(jsonString;id1:value1) not equaling -1 for both IDs.

Attempt Two: Checking the array using an iterator and adding the item to the list.

Subsequently, I converted the jsonString to JSON and utilized an Iterator to filter based on the two conditions: id1 equals value1 and id2 equals value2.

However, when I attempt to add the item to the JSON object and then convert it back to a JSON string for sending, the formatting appears incorrect. I verified this using Slack, where the JSON string object I added was "{"id1":"value1","id2":"value2"}" whereas all other items in the jsonstring were formatted as {"id1":"value1,"id2":value2"}.

I'm unsure about what I'm doing incorrectly, and I suspect I might be overcomplicating the workflow. Any assistance would be greatly appreciated. Thank you.

 
Posted : 13/05/2024 9:49 am
Share: