Select to view content in your preferred language

Calculate Fields simple python expression

415
1
01-12-2024 11:35 AM
Labels (1)
ElijahLebo
New Contributor

I am writing a small script taking Excel to a point layer in ArcGIS Pro. My question involves a small expression parsing the x and y coordinates from a single long string. The string is formatted as follows:

{"AddressCandidates":[{"location":{"spatialReference":{"wkid":4326},"x":-88.32831272050885,"y":42.00719348709284}}],"SelectedCandidate":{"location":{"spatialReference":{"wkid":4326},"x":-88.32831272050885,"y":42.00719348709284}},"LastGeocodedAddress":null

 

I am using the calculate fields (multiple). I cannot use string length because it varies. I think I can identify the "x": and "y":  using message.find

 

I am a student trying to learn, any help would be appreciated.

 

1 Reply
DavidPike
MVP Frequent Contributor

Your data is in a format called JSON which can be unpacked easily.

 

import arcpy
import json

your_string = 'your JSON coordinate string'

data = json.loads(your_string)

x = data["AddressCandidates"][0]["location"]["x"]
y = data["AddressCandidates"][0]["location"]["y"]