1

I have some unstructured JSON that looks like so:

{
    "Entities": [{
        "BeginOffset": 19, "EndOffset": 32, "Score": 2.0,
        "Text": "WASHINGTON", "Type": "LOCATION"
    }, {
        "BeginOffset": 33, "EndOffset": 35, "Score": 1, 
        "Text": "Ha", "Type": "LOCATION"
    }, {
        "BeginOffset": 36, "EndOffset": 39, "Score": 2.2,
        "Text": "JAN", "Type": "LOCATION"
    }], 
    "File": "sample.txt",
    "Line": 11
}

I upload this into Elasticsearch for use in Kibana - but the geotagging feature gives WASHINGTON the coordinates for WA state, not our esteemed capital city, the District of Columbia. I need DC coordinates.

The above JSON has WASHINGTON all through it - and I want to change every WASHINGTON to District of Columbia. I'm using the Google Maps API to geotag locations in the JSON:

if str(json_package['document']['Entities'][0]['Type']) == "LOCATION":
    geocode_result = gmaps.geocode(
        json_package['document'] ['Entities'][0]['Text'])

    lat_long = {
        'lat': geocode_result[0]['geometry']['location']['lat'],
        'lon': geocode_result[0]['geometry']['location']['lng']
    }

    # print(lat_long) 

How can I update this? The sample above shows how I changed all instances of lng to lon, but the same thing does not work for the city. I've been going off this link

I also realize that changing text would have to happen before the actual geocoding

wbadart
  • 2,583
  • 1
  • 13
  • 27
papelr
  • 468
  • 1
  • 11
  • 42
  • You say "unstructured JSON", then you accept an answer that makes assumptions about structure? If you know that the "Type" and "Text" will be under an entry in "Entities", that's structure. – Charles Duffy Dec 24 '18 at 17:55
  • BTW, doing this with `jq` 1.6 (in a *truly* unstructured way) might look like `walk(if . == "WASHINGTON" then "District of Columbia" else . end)` – Charles Duffy Dec 24 '18 at 17:58

1 Answers1

1

Sounds like you should be able to look at each of those entities and update them in place as needed:

# Let data = your unstructured JSON object
for entity in data['Entities']:
    if entity['Type'] == 'LOCATION' and entity['Text'] == 'WASHINGTON':
        entity['Text'] = 'District of Columbia'

demo

wbadart
  • 2,583
  • 1
  • 13
  • 27