Working Nested Json
Question

I have attached a Rest service that comes from a third party and it returns a JSON string.


The String is for Services and contains a Machine - Which has a list of nested Service Tasks - Which in turn has a list of parts required for each task.


Most of the users will be offline when using the app so i need to take that data and store it in Local Storage. I have created the Local Storage Structures but I am unclear how to separate the Json out into the individual tables.


I also need help with rebuilding the JSON when I want to write back to the REST API.


Any help or point in the right direction will be greatly appreciated.


Frosty

Sample of the JSON Return 

{

  "service": {

    "id": 1,

    "machineID": 1,

    "machineName": "test machine",

    "machineSerialNo": "TNP-111-222-333",

    "serviceStatus": "Open",

    "serviceStartDate": "2021-06-01T00:00:00",

    "serviceEndDate": "2021-06-08T00:00:00",

    "notes": "          "

  },

  "serviceTaskList": [

    {

      "id": 1,

      "serviceId": 1,

      "currentAssemblyID": 1,

      "currentAssemblyName": "Test Assembly",

      "taskName": "Clean doofers",

      "taskQuoteName": "Clean Doofers",

      "taskDescription": "Clena the doofers",

      "notes": " ",

      "serviceTaskPartList": [

        {

          "id": 1,

          "taskId": 1,

          "partId": 1,

          "partName": "Test Part 1",

          "qtyNeeded": 50,

          "qtyUsed": 35,

          "qtyReturned": 10,

          "qtyLeftOnSite": 5

        },

        {

          "id": 2,

          "taskId": 1,

          "partId": 2,

          "partName": "Test Part 2",

          "qtyNeeded": 500,

          "qtyUsed": 499,

          "qtyReturned": 1,

          "qtyLeftOnSite": 0

        }

      ]

    },

    {

      "id": 2,

      "serviceId": 1,

      "currentAssemblyID": 2,

      "currentAssemblyName": "Test Assembly two",

      "taskName": "Vacuum Buffers",

      "taskQuoteName": "Vacuum Buffers",

      "taskDescription": "Vucuum the buffers",

      "notes": " ",

      "serviceTaskPartList": [

        {

          "id": 3,

          "taskId": 2,

          "partId": 3,

          "partName": "Test Part 3",

          "qtyNeeded": 5,

          "qtyUsed": 3,

          "qtyReturned": 1,

          "qtyLeftOnSite": 1

        }

      ]

    }

  ]


}

mvp_badge
MVP
Solution

Hi Mat,

You can do this the common way and create a datamodel, with 3 entities:

Service:

  • id (NO AutoNumber, Primary key)
  • machineID
  •  machineName
  •  machineSerialNo
  •  serviceStatus
  •  serviceStartDate
  •  serviceEndDate
  •  notes

ServiceTask:

  • Id (NO Autonumber, Primary key)
  • ServiceId (foreigh key to Service entity)
  • CurrentnAssemblyId
  • CurrentAssemblyName
  • TaskName
  • TaskQuoteNAme
  • TaskDescription
  • Notes

 ServiceTaskPart:

  • Id (NO Autonumber, Primary key)
  • ServiceTaskPartId (foreigh key to ServiceTask entity)
  • TaskId
  • PartId
  • PartName
  • QtyNeeded
  • QtyUsed
  • QtyReturned
  • QtyLeftOnSide

When you get the REST response:

  1. Use a For each on Service in the returned structure, and CreateOrUpdate the Service Entity
  2. In that Foreach use another ForEach to loop through the ServiceTaskList and CreateOrUpdate the ServiceTasks
  3. In that Foreach use another ForEach to loop through the ServiceTasksPartsList and CreateOrUpdate the ServiceTaskParts.

Than you need to create an offline data model of the above 3 entities, you can start with the same entities, by copy pasting the definition to the local entities, but remove all the attributes that you don't need locally.

 Then use the offline sync patterns to synchronize the data from the server entities to the local entities and back again. 

Some reading material:

Alternatively, you can also create one server and local entity, which will store one record with one binary attribute to store the complete response. In terms of offline data sync this is less complicated.

On the mobile device you would read the service record, convert the binary attribute to text and then transform the JSON text to a structure that is equal to the JSON response. This way of work requires a different approach in handling the data entry in your mobile app and is less useful if you do not need all the data of the API response locally.

Regards,

Daniel

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.