Json Web Service - GeoEvent

1518
4
03-08-2017 06:57 AM
WellingtonChristian_Rodrigues_
Esri Contributor

Hi everybody I have doubts about how to consume data from a Json web service. I'm going to explain.

I'm trying to consume a Json web service with tracking of buses, so as my input I set as External Website for Json, that a set as the attached file.

When I open the URL on the browser I can see the data, and is like this:

{"COLUMNS":["DATAHORA","ORDEM","LINHA","LATITUDE","LONGITUDE","VELOCIDADE"],"DATA":[["07-03-2017 17:57:31","444014","933.3",-15.84587,-48.122343,23],["07-03-2017 17:57:40","444219","363.1",-15.84801,-48.115563,46],["07-03-2017 17:57:43","444251","933.2",-15.809476,-48.088196,49],["07-03-2017 17:57:31","443956","0.043",-15.803197,-48.025253,26],["07-03-2017 17:57:32","444057","933.1",-15.822888,-48.11473,40],["07-03-2017 17:57:16","444294","933.3",-15.804666,-48.072663,27],["07-03-2017 17:57:12","443913","363.2",-15.820277,-48.107763,14],["07-03-2017 17:57:11","442585","0.378",-15.832833,-48.05481,19],["07-03-2017 17:57:32","443034","0.902",-15.787903,-47.895599,35],["07-03-2017 17:57:47","444171","933.1",-15.804333,-48.07237,18],["07-03-2017 17:31:40","444006","933.1",-15.843577,-48.124094,25],["07-03-2017 17:46:01","444162","0.043",-15.84009,-48.12461,16],["07-03-2017 17:37:35","444260","933.2",-15.840137,-48.12673,39],["07-03-2017 14:35:57","444197","933.1",-15.839257,-48.12737,20]

Just to see what's coming to geoevent I set TCP Socket as my output, with TCPServerApp and I could see only the columnseach five seconds with no data, like this:

08/03/17 10:46 : MARECHAL_JSON,"[DATAHORA, ORDEM, LINHA, LATITUDE, LONGITUDE, VELOCIDADE]", 08/03/17 10:46 : MARECHAL_JSON,"[DATAHORA, ORDEM, LINHA, LATITUDE, LONGITUDE, VELOCIDADE]", 08/03/17 10:46 : MARECHAL_JSON,"[DATAHORA, ORDEM, LINHA, LATITUDE, LONGITUDE, VELOCIDADE]", 08/03/17 10:46 : MARECHAL_JSON,"[DATAHORA, ORDEM, LINHA, LATITUDE, LONGITUDE, VELOCIDADE]", 08/03/17 10:46 : MARECHAL_JSON,"[DATAHORA, ORDEM, LINHA, LATITUDE, LONGITUDE, VELOCIDADE]",

Am I doing some thing wrong?

How should I use Header Parameter Name and Json Object Name?

Does someone have some tips?

Thank you

Wellington Alves

0 Kudos
4 Replies
ChrisBeyett
Occasional Contributor

Wellington, 

When GeoEvent reads a website for JSON, it uses the first object as the starting point for the message. The behavior you are seeing is typical when multiple elements are present in the JSON. To fix it, you should be able to set the Json Object Name parameter to "DATA". This will tell the input connector to start the message at the DATA element. 

Thanks,
Chris

WellingtonChristian_Rodrigues_
Esri Contributor

Hi Chris,

Tks for your answer, but still not working.

When I set the field Json Object Name with DATA or "DATA", the event stops to coming and I'm not able to see the headers through TCPServerApp.

Do you have any other idea?

Thanks

Wellington

0 Kudos
AlexanderBrown5
Occasional Contributor II

Wellington,

Is your external website publicly available?  I would like to take a look since Chris' answer should have solved it for you.

~Alex

WellingtonChristian_Rodrigues_
Esri Contributor

Hi Alexander,

The Website is available to a specific IP adress, so It's not a public access. I could solve this creating a python script that generate a .csv file. The python script is like this below:

import requests
import json
import csv
import os
import datetime
import time
from time import gmtime, strftime

while True:

    url = 'http://<server>:13130/InLog/GetTempoReal'
    r = requests.get(url,stream=True)

    if r.encoding is None:
        r.encoding = 'utf-8'
        
    #print r.status_code

    content = r.json()
    #print(content.keys())

    #columns = str(content[u'COLUMNS'])
    data = str(content[u'DATA'])

    #print data

    data_temp1 = data.replace('[[u','')
    data_temp2 = data_temp1.replace('u','')
    data_temp3 = data_temp2.replace('[','')
    data_temp4 = data_temp3.replace("'","")

    lista_data = data_temp4.split('], ')

    #print lista_data[0]
    #print lista_data[1]

    listaParaGerarCsv = []
    for row in lista_data:
        valores = row.split(', ')
        #print valores
        lista_Interna = []
        for valor_temp in valores:
            valor = valor_temp.replace('.',',')
            lista_Interna.append(valor)
            #print lista_Interna
        listaParaGerarCsv.append(lista_Interna)
        
    unidade='c'
    diretorioBase=''

    if os.name == 'nt':
        diretorioBase=unidade+':'
    else:
        diretorioBase=os.getenv("HOME")

    caminhoAbsoluto = diretorioBase + os.sep + 'temp\dir\CSV' + os.sep
    #caminhoAbsoluto = diretorioBase + os.sep + 'dir\CSV' + os.sep
    if not os.path.exists(caminhoAbsoluto):
        os.makedirs(caminhoAbsoluto)

    arquivoOutput = caminhoAbsoluto + 'posicionamento' + strftime("%Y%m%d%H%M%S", gmtime()) + '.csv'

    if not os.path.exists(arquivoOutput):
        with open(arquivoOutput, 'wb') as csvfile:
            spamwriter = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL)
            spamwriter.writerow(['datahora', 'ordem', 'linha', 'latitude', 'longitude', 'velocidade'])
            for linha in listaParaGerarCsv:
                spamwriter.writerow(linha)

        print 'Arquivo gerado no diretorio: ' + arquivoOutput

    time.sleep(10)

Thanks

Wellington