Select to view content in your preferred language

Extracting a nested attribute from geojson in velocity using Arcade

887
4
10-22-2024 11:02 AM
NateRogers1
Emerging Contributor

I have a geojson live feed in Velocity. The data comes in fine, however my main core of attrbutes are all nested together in one attribute column. I know in Velocity, I can add fields, but I do not know the arcade functions needed to break down the nested attributes into their own new fields. Can someone help me with the arcade?

 

currentConditions[{"id":3023658,"conditionId":17,"conditionDescription":"3 - dry","userName":"","startTime":1729576800213,"endTime":4883216432298,"confirmationTime":1729616432298,"updateTime":1729576800213,"confirmationUserName":"","sourceType":"OPERATOR","additionalData":null},{"id":3026261,"conditionId":47,"conditionDescription":"forecast text included","userName":"","startTime":1729616555832,"endTime":1729638039767,"confirmationTime":1729616439767,"updateTime":1729616439767,"confirmationUserName":"","sourceType":"NDFD","additionalData":"Clear skies. Wind gusts of up to 21 mph are expected."}]
0 Kudos
4 Replies
Joanna_Nishimura
Esri Contributor

Hi @NateRogers1- I think there are 2 ways that you could approach this:

  1. Change the feed configuration slightly.  If you're using an HTTP Poller as your feed, you can change from GeoJSON to JSON in the feed configuration.  This will expose additional options for you to flatten your objects and also flatten your arrays.  If there are certain arrays and objects that you don’t want flattened, you can also add exceptions by clicking on the gear icons. I the below screenshot, I used your sample and changed the Data format to JSON, enabled Flatten, and enabled Flatten arrays. 

Joanna_Nishimura_0-1729725981265.png

2. Go the route that you initially suggested an use some Arcade. You could use the Calculate Field tool and set up multiple calculations to parse your field, currentConditions, into separate fields. I'm guessing your field currentConditions is a String field, so you need to first convert it into an array, which then makes parsing a lot easier.  Here's some example Arcade:   

 

// my field called 'placeholder' contains the array with nested objects.  This field is a Text field
// use fromJSON to convert text into an Array
var currentConditions = fromJSON($feature.placeholder);

// array is stored in var currentConditions.  This contains 2 objects
// access first object (at index value 0), then access value associated with field 'conditionId'.  This returns 17
var conditionId_1 = currentConditions[0]['conditionId']

// access second object (at index value 1), then access value associated with field 'additionalData'.  This returns 'Clear skies.  Wind...'
var additionalData_2 = currentConditions[1]['additionalData'];
return additionalData_2;

 

 

-Joanna

NateRogers1
Emerging Contributor

Hi Joanna,

 

    Thank you so much for taking the time to help. I want to keep the feed as a geojson because there are a mix of points and polylines and if I turn the feed into a json, those extra coordinates needed for the lines disappear as it only becomes a begin xy to end xy and doesnt pick up all the middle verticies.

 

I am completely terrible at Arcade, do you know of any resources or would be able to help parse out more what the arcade function would look like? Thanks!!!

0 Kudos
Joanna_Nishimura
Esri Contributor

Hi Nate- I totally understand, Arcade can be a little bit of a learning curve.  

I would start with the function reference documentation here.  If you're looking to learn more about parsing data returned in an array, maybe first focus on the Array functions area first.

Another quick resource that I like to use is the ArcGIS Arcade Playground.  In the screenshot below, you can see how I took the Arcade snippet that I provided, and replaced the currentConditions variable with the text that you initially provided.  Then I'm able to execute the Run the expression and change my expression to change the output results.  

Joanna_Nishimura_0-1730833152291.png

 

Some other Velocity-specific resources for Arcade:

Using Arcade in ArcGIS Velocity

Best practices for Arcade expressions in ArcGIS Velocity 

 

-Joanna

NateRogers1
Emerging Contributor

so I was struggling with this and I had an idea that seems to work. Instead of struggling with the arcade, I can connect to the feed twice, once as a geojson to get the geometry but exclude out all attributes except the id field, then I can create a new feed as a JSON so I can flatten the attributes but with no geometry then in Velocity I can use the join field to join the geojson ID field with the JSON id field so I get both geojson geometries with the fixed attributes.