Write in file >> problem with json format

Post your questions and help other users.

Moderator: Martin

Post Reply
stefanprivat
Posts: 24
Joined: 28 Jan 2019 12:42

Write in file >> problem with json format

Post by stefanprivat » 28 Feb 2019 14:09

Hi all

Have a problem with json format.
Currently, I log GPS data, cellID and LAC and write this info periodically in a text file with action "write to file" >> all works fine.

Text:
{"\n"}{location,locationformat,decimal},{cid},{lac},{getDate(),dateformat,dd.MM.yyyy,HH:mm};


BUT:
Now I should have the data written in this file in geojson format.
So I thought, I can do this like follows (with the variables):
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": {location,locationformat,decimal}},
"properties": {
"cellID": {cid},
"lac": {lac},
"date": {getDate(),dateformat,dd.MM.yyyy,HH:mm}
}
}

]
}


But the only what I get in this file is:
{error}

Anybody can help me? Sorry, I am really not the programmer specialist, therefore upper question.
Thanks a lot in advance!!!

The flow log shows no error:
28.02.2019 14:49:39.432 [4_Save gps cid lac to file] Starting to execute flow '4_Save gps cid lac to file' with Context{global{global_prevloc=Location[gps 46.947200,7.389944 hAcc=48 et=+5d19h34m45s117ms alt=603.4882866595597 vel=0.37801284...},local{flow_name=4_Save gps cid lac to file,trigger=Manual,triggertime=1551361779427}}
28.02.2019 14:49:39.435 [4_Save gps cid lac to file] Start executing action 'Initialisiere Variable Standort: Neuer Standort in location durch GPS, Timeout 2m'
28.02.2019 14:49:47.811 [4_Save gps cid lac to file] Action 'Initialisiere Variable Standort: Neuer Standort in location durch GPS, Timeout 2m' Variable 'location' set to value 'Location[gps 46.947190,7.389795 hAcc=48 et=+5d19h59m54s183ms alt=670.3619005996284 vel=0.11095798 bear=301.61996 vAcc=??? sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=40]}]'.
28.02.2019 14:49:47.828 [4_Save gps cid lac to file] End executing action 'Initialisiere Variable Standort: Neuer Standort in location durch GPS, Timeout 2m'
28.02.2019 14:49:47.841 [4_Save gps cid lac to file] Flow continues executing with the next step.
28.02.2019 14:49:47.842 [4_Save gps cid lac to file] Start executing condition 'Funkzelle GSM: Verbunden mit LACs oder CIDs '
28.02.2019 14:49:47.885 [4_Save gps cid lac to file] End executing condition 'Funkzelle GSM: Verbunden mit LACs oder CIDs ' with return value false
28.02.2019 14:49:47.907 [4_Save gps cid lac to file] Flow continues executing with the next step.
28.02.2019 14:49:47.907 [4_Save gps cid lac to file] Start executing action 'Script: if (isEmpty({global_prevloc})) {global_prevloc = {location} }'
28.02.2019 14:49:47.922 [4_Save gps cid lac to file] End executing action 'Script: if (isEmpty({global_prevloc})) {global_prevloc = {location} }'
28.02.2019 14:49:47.940 [4_Save gps cid lac to file] Flow continues executing with the next step.
28.02.2019 14:49:47.940 [4_Save gps cid lac to file] Start executing condition 'Expression: distance(location, global_prevloc)>1'
28.02.2019 14:49:47.956 [4_Save gps cid lac to file] End executing condition 'Expression: distance(location, global_prevloc)>1' with return value true
28.02.2019 14:49:47.972 [4_Save gps cid lac to file] Flow continues executing with the next step.
28.02.2019 14:49:47.973 [4_Save gps cid lac to file] Start executing action 'In Datei Schreiben: {
    "type": "FeatureCollection",
    "features": [
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": {location,locationformat,decimal}},
        "properties": {
          "cellID": {cid},
          "lac": {lac},
                          "date": {getDate(),dateformat,dd.MM.yyyy,HH:mm}
        }
      }
 
    ]
  }
in /storage/emulated/0/Automagic/writelocfile.txt (anhängen)'
28.02.2019 14:49:47.990 [4_Save gps cid lac to file] End executing action 'In Datei Schreiben: {
    "type": "FeatureCollection",
    "features": [
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": {location,locationformat,decimal}},
        "properties": {
          "cellID": {cid},
          "lac": {lac},
                          "date": {getDate(),dateformat,dd.MM.yyyy,HH:mm}
        }
      }
 
    ]
  }
in /storage/emulated/0/Automagic/writelocfile.txt (anhängen)'
28.02.2019 14:49:48.005 [4_Save gps cid lac to file] Flow continues executing with the next step.
28.02.2019 14:49:48.006 [4_Save gps cid lac to file] Start executing action 'Script: global_prevloc = location'
28.02.2019 14:49:48.022 [4_Save gps cid lac to file] End executing action 'Script: global_prevloc = location'
28.02.2019 14:49:48.038 [4_Save gps cid lac to file] Flow ended.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Write in file >> problem with json format

Post by Desmanto » 28 Feb 2019 17:34

I don't think it will work that way. Don't construct the json directly. Create the object using list/map first, then convert it to json afterward. The one you need probably can be defined by using this script.

Code: Select all

location = newLocation(123.456789, 98.765432);
cid = 112233;
lac = 456;

object = newMapFromValues(
"type", "FeaturesCollection",
"features", newList(newMapFromValues(
  "geometry", newMapFromValues(
     "type", "Point",
     "coordinates", "{location,locationformat,decimal}"),
  "properties", newMapFromValues(
     "cellID", cid,
     "lac", lac,
     "date", "{getDate(),dateformat,dd.MM.yyyy,HH:mm}") ) ) );

write = toJSON(object);
I purposely indented the script to make it easier to see the nested element. I create dummy data for location, cid and lac.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

stefanprivat
Posts: 24
Joined: 28 Jan 2019 12:42

Re: Write in file >> problem with json format

Post by stefanprivat » 01 Mar 2019 12:09

Dear Desmanto

Many thanks for the great support here!
I had to change 2-3 small things (split the geoCoordinaten, replace the "=" with ":" ...) after that,
the output in the file was compatible with geojason format!

So, it is a great fun to use Automagic!

!!!Merci viel Mal!!!

Post Reply