Export-Import via json and API

I’m not a computer scientist but
I worked with the API
here
https://farmos.discourse.group/t/api-problem-with-curl/317/16
Trying to understand how to enter multiple logs at the same time
I imagined that this could also be the best way to do an export-import
also responding to topics like this
https://farmos.discourse.group/t/download-farm-info/131/2
For exports then it would be enough to export a .json
for example with this command:

curl -X GET --cookie farmOS-cookie.txt -H “X-CSRF-Token: ${TOKEN}” [URL]/log.json

and then import it with curl -X POST
but I’m a little ignorant of the JSON syntax
do you have to build an array?
If the result is like this

{“self”:“https://[URL]/log”,“first”:“https://[URL]/log?page=0”,“last”:“https://[URL]/log?page=0”,
“list”:[
{
“membership”:{“group”:},“inventory”:,“movement”:{“area”:,“geometry”:null},“quantity”:,“id”:“17”,
“name”:“Observation 05/01/2020 - 10:19”,“type”:“farm_observation”
,“uid”:{“uri”:“https://[URL]/user/3”,“id”:“3”,“resource”:“user”},“timestamp”:“1588321152”,“created”:“1588321152”,“changed”:“1588321158”,“done”:“1”,“url”:“https://[URL]/log/17”,“asset”:,“files”:,“images”:,“log_category”:,“log_owner”:[{“uri”:“https://[URL]/user/3”,“id”:“3”,“resource”:“user”}],“notes”:,“flags”:,“geofield”:,“area”:,“equipment”:,“data”:null
},
{
“membership”:{“group”:},“inventory”:,“movement”:{“area”:,“geometry”:null},“quantity”:,“id”:“18”,
“name”:“Observation 05/01/2020 - 10:20”,“type”:“farm_observation”
,“uid”:{“uri”:“https://[URL]/user/3”,“id”:“3”,“resource”:“user”},“timestamp”:“1588321206”,“created”:“1588321206”,“changed”:“1588321210”,“done”:“1”,“url”:“https://[URL]/log/18”,“asset”:,“files”:,“images”:,“log_category”:,“log_owner”:[{“uri”:“https://[URL]/user/3”,“id”:“3”,“resource”:“user”}],“notes”:,“flags”:,“geofield”:,“area”:,"equipment,“data”:null}]
}

it’s actually not enough to import it as it is in a -X POST curl
at least I tried it and it doesn’t work

2 Likes

What are you trying to do, exactly? Are you trying to export from one farmOS and import into another?

Using the JSON API for this will require some extra work. Notice that all records have an ID. This ID is going to be auto-generated by the farmOS instance, so when you create a new log it receives an ID automatically.

If you include the ID in the JSON that you are POSTing, it will not create a new log. But that is how you can update existing logs. The API docs explain this: https://farmos.org/development/api

If you are moving logs to another farmOS, you can clear out the ID before POSTing it, but note that a log may also reference other records. And so those records will have their own IDs as well. You need to make sure those records are created BEFORE you reference them with a new log. And that means you’ll need to maintain an mapping index of the old IDs and new IDs.

I would recommend using the farmOS Python library if you are trying to do something advanced like this. https://github.com/farmOS/farmOS.py

Also worth noting: in farmOS 2.x (currently in development), all records will have UUIDs (universally unique IDs) which will make this process easier.

Hope that helps!

Here’s a script to create Observation logs from a JPEG image GPS/timestamp metadata: API problem with curl (posted in that topic before I saw this one)

But yeah, I agree with @mstenta farmOS.py should work great for many of these tasks! I’m curious how to make farmOS.py more approachable for folks that are less tech savy, too. Setting up a Python environment isn’t too hard, but certainly is a barrier. Couple ideas… how can we…

  1. Publish & share simple scripts related to farmOS
  2. Wrap scripts in a more user-friendly CLI (is it possible to package all the dependencies together? perhaps with https://typer.tiangolo.com/ ?)
1 Like

Maybe Postman would be a good option?

1 Like