0

I have a json file (nested json) that I am unmarshalling its content into a map[string]interface. Now I have to implement pagination as the data is large. The client side will send as a query parameter the desired page, how can I slice the data I have? This is a snippet of the data I am dealing with:

"packages":{
  "pkg1": {
    "meta": {
      "description": "description1",
      "name": "pkg1.1"
    },
    "name": "pkg1.1"
  },
  "pkg2": {
    "meta": {
      "description": "description2",
      "name": "pkg2.2"
    },
    "name": "pkg2.2"
  },
}

So what I did is that I recursively iterated through the data and created an array of a custom type containing the data I need (name, description) for each entry so that I can use it for pagination. Here is the code I used:

type Object struct {
    name string
    description string
}

func iterate(aMap map[string]interface{}, result *[]Object){
for key, val := range aMap {
    switch val.(type) {
        case map[string]interface{}:
            if(key == "meta"){
                switch reflect.TypeOf(val).Kind() { 
                    case reflect.Map:
                        s := reflect.ValueOf(val)
                        var tmpData Object
                        if(s.MapIndex(reflect.ValueOf("name")).IsValid()){
                            tmpData.name = s.MapIndex(reflect.ValueOf("name")).Interface().(string)
                        }

                        if(s.MapIndex(reflect.ValueOf("description")).IsValid()){
                            tmpData.description = s.MapIndex(reflect.ValueOf("description")).Interface().(string)
                        }
                        *result = append(*result, tmpData)
                }
            }
            iterate(val.(map[string]interface{}), result)
        default: //DO NOTHING!!
        }
}
}
3
  • 2
    What have you tried? Try something and come back when you get an error. Commented Apr 16, 2018 at 12:59
  • You want to slice the data before unmarshalling, or slice the unmarshalled result? Commented Apr 16, 2018 at 13:10
  • Slice the unmarshalled result Commented Apr 16, 2018 at 13:17

2 Answers 2

2

If you're doing pagination, somewhere the data must be represented as a list instead of an object? I assume at some place in your JSON, you have a list of items, otherwise pagination doesn't make sense.

It shouldn't be very hard, something simple like this should work:

const (
    itemsPerPage = 10
)

var data []map[string]interface{}

// pages start at 1, can't be 0 or less.
func GetDataPage(page int) []map[string]interface{} {
    start := (page - 1) * itemsPerPage
    stop := start + itemsPerPage

    if start > len(data) {
        return nil
    }

    if stop > len(data) {
        stop = len(data)
    }

    return data[start:stop]
}
Sign up to request clarification or add additional context in comments.

2 Comments

I updated my question with a sample of the data. I need the name and description to be an entry (a index of a page). So as you can see, I am only dealing with objects, and I should find a way to extract {"name": "name1", "description": "description1"} and append it to an array so that I can index it for pagination.
I updated my question with the code I used to transform the data I need into an array that supports indexing. And I used the code you provided and it is working now. Thank you!
0

You are unmarshalling your json into a map which has no order by itself. In order to be able to paginate your results you need to order them in some way.

One way of doing it is to sort your data and then store it into an array. But in order to paginate you need to have ordered data and that is not possible with a map.

1 Comment

That's exactly what I am working on right now, I need to extract data and push it into an array so that I can use the index for pagination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.