GoLang Redis : Map & Slice





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I'm using GoLang to get a data from redis hash and then map into a struct.



type Person struct {
ID string `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Filters interface{} `json:"filters"`
Type string `json:"type"`
}


In Redis, a hash field contains a stringified JSON.




HGET hashname fieldname




Above returns a stringified JSON.



Now "filters" key can be array or map based on the type (That's why I defined Filters type as interface in struct).

I marshall the JSON like below:



var p Person
content, err := redis.HGet("hashName", "id").Result()
_ = json.Unmarshal(byte(content), &p)


Now I have to loop over filters like below but this is giving error that cannot range over p.Filters (type interface {}) (I understand why this error is coming)



for _, filter := range p.Filters {
fmt.Println(filter)
}


Is there any way we can handle this situation?



Thanks,

Shashank










share|improve this question


















  • 1





    Are you asking how to convert JSON text to Go values where an element of the JSON can be an array or object? If so, give examples of the two variants of the JSON. Redis seems to be unrelated to the problem.

    – Cerise Limón
    Nov 16 '18 at 18:02




















-1















I'm using GoLang to get a data from redis hash and then map into a struct.



type Person struct {
ID string `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Filters interface{} `json:"filters"`
Type string `json:"type"`
}


In Redis, a hash field contains a stringified JSON.




HGET hashname fieldname




Above returns a stringified JSON.



Now "filters" key can be array or map based on the type (That's why I defined Filters type as interface in struct).

I marshall the JSON like below:



var p Person
content, err := redis.HGet("hashName", "id").Result()
_ = json.Unmarshal(byte(content), &p)


Now I have to loop over filters like below but this is giving error that cannot range over p.Filters (type interface {}) (I understand why this error is coming)



for _, filter := range p.Filters {
fmt.Println(filter)
}


Is there any way we can handle this situation?



Thanks,

Shashank










share|improve this question


















  • 1





    Are you asking how to convert JSON text to Go values where an element of the JSON can be an array or object? If so, give examples of the two variants of the JSON. Redis seems to be unrelated to the problem.

    – Cerise Limón
    Nov 16 '18 at 18:02
















-1












-1








-1








I'm using GoLang to get a data from redis hash and then map into a struct.



type Person struct {
ID string `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Filters interface{} `json:"filters"`
Type string `json:"type"`
}


In Redis, a hash field contains a stringified JSON.




HGET hashname fieldname




Above returns a stringified JSON.



Now "filters" key can be array or map based on the type (That's why I defined Filters type as interface in struct).

I marshall the JSON like below:



var p Person
content, err := redis.HGet("hashName", "id").Result()
_ = json.Unmarshal(byte(content), &p)


Now I have to loop over filters like below but this is giving error that cannot range over p.Filters (type interface {}) (I understand why this error is coming)



for _, filter := range p.Filters {
fmt.Println(filter)
}


Is there any way we can handle this situation?



Thanks,

Shashank










share|improve this question














I'm using GoLang to get a data from redis hash and then map into a struct.



type Person struct {
ID string `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Filters interface{} `json:"filters"`
Type string `json:"type"`
}


In Redis, a hash field contains a stringified JSON.




HGET hashname fieldname




Above returns a stringified JSON.



Now "filters" key can be array or map based on the type (That's why I defined Filters type as interface in struct).

I marshall the JSON like below:



var p Person
content, err := redis.HGet("hashName", "id").Result()
_ = json.Unmarshal(byte(content), &p)


Now I have to loop over filters like below but this is giving error that cannot range over p.Filters (type interface {}) (I understand why this error is coming)



for _, filter := range p.Filters {
fmt.Println(filter)
}


Is there any way we can handle this situation?



Thanks,

Shashank







json go redis






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 17:38









Shashank SachanShashank Sachan

8218




8218








  • 1





    Are you asking how to convert JSON text to Go values where an element of the JSON can be an array or object? If so, give examples of the two variants of the JSON. Redis seems to be unrelated to the problem.

    – Cerise Limón
    Nov 16 '18 at 18:02
















  • 1





    Are you asking how to convert JSON text to Go values where an element of the JSON can be an array or object? If so, give examples of the two variants of the JSON. Redis seems to be unrelated to the problem.

    – Cerise Limón
    Nov 16 '18 at 18:02










1




1





Are you asking how to convert JSON text to Go values where an element of the JSON can be an array or object? If so, give examples of the two variants of the JSON. Redis seems to be unrelated to the problem.

– Cerise Limón
Nov 16 '18 at 18:02







Are you asking how to convert JSON text to Go values where an element of the JSON can be an array or object? If so, give examples of the two variants of the JSON. Redis seems to be unrelated to the problem.

– Cerise Limón
Nov 16 '18 at 18:02














1 Answer
1






active

oldest

votes


















1














You need to convert Filters type interface{} into the expected slice. If you don't really know what type it will be, you can use map[string]interface{}. Therefore change your Filters type to map[string]interface{}.



Per more information
If Filters can be an array (or not), then you might consider a type switch:



A simple example:



package main

import (
"encoding/json"
"fmt"
)

func main() {
var i interface{}
//json.Unmarshal(byte(`["hi"]`), &i)
json.Unmarshal(byte(`{"a":"hi"}`), &i)

switch i.(type) {
case interface{}:
println("ARRAY")
case map[string]interface{}:
println("NOT ARRAY")
default:
fmt.Printf("%Tn", i)
}
}





share|improve this answer


























  • If I use map[string]interface{} then I cannot assign array to Filters. (Filters can be array or map)

    – Shashank Sachan
    Nov 16 '18 at 17:47











  • I see. I'll update my answer.

    – poy
    Nov 16 '18 at 17:52











  • Thanks! This is going to work for me.

    – Shashank Sachan
    Nov 16 '18 at 18:58











  • Awesome! I'm glad I could help.

    – poy
    Nov 16 '18 at 19:00












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342831%2fgolang-redis-map-slice%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You need to convert Filters type interface{} into the expected slice. If you don't really know what type it will be, you can use map[string]interface{}. Therefore change your Filters type to map[string]interface{}.



Per more information
If Filters can be an array (or not), then you might consider a type switch:



A simple example:



package main

import (
"encoding/json"
"fmt"
)

func main() {
var i interface{}
//json.Unmarshal(byte(`["hi"]`), &i)
json.Unmarshal(byte(`{"a":"hi"}`), &i)

switch i.(type) {
case interface{}:
println("ARRAY")
case map[string]interface{}:
println("NOT ARRAY")
default:
fmt.Printf("%Tn", i)
}
}





share|improve this answer


























  • If I use map[string]interface{} then I cannot assign array to Filters. (Filters can be array or map)

    – Shashank Sachan
    Nov 16 '18 at 17:47











  • I see. I'll update my answer.

    – poy
    Nov 16 '18 at 17:52











  • Thanks! This is going to work for me.

    – Shashank Sachan
    Nov 16 '18 at 18:58











  • Awesome! I'm glad I could help.

    – poy
    Nov 16 '18 at 19:00
















1














You need to convert Filters type interface{} into the expected slice. If you don't really know what type it will be, you can use map[string]interface{}. Therefore change your Filters type to map[string]interface{}.



Per more information
If Filters can be an array (or not), then you might consider a type switch:



A simple example:



package main

import (
"encoding/json"
"fmt"
)

func main() {
var i interface{}
//json.Unmarshal(byte(`["hi"]`), &i)
json.Unmarshal(byte(`{"a":"hi"}`), &i)

switch i.(type) {
case interface{}:
println("ARRAY")
case map[string]interface{}:
println("NOT ARRAY")
default:
fmt.Printf("%Tn", i)
}
}





share|improve this answer


























  • If I use map[string]interface{} then I cannot assign array to Filters. (Filters can be array or map)

    – Shashank Sachan
    Nov 16 '18 at 17:47











  • I see. I'll update my answer.

    – poy
    Nov 16 '18 at 17:52











  • Thanks! This is going to work for me.

    – Shashank Sachan
    Nov 16 '18 at 18:58











  • Awesome! I'm glad I could help.

    – poy
    Nov 16 '18 at 19:00














1












1








1







You need to convert Filters type interface{} into the expected slice. If you don't really know what type it will be, you can use map[string]interface{}. Therefore change your Filters type to map[string]interface{}.



Per more information
If Filters can be an array (or not), then you might consider a type switch:



A simple example:



package main

import (
"encoding/json"
"fmt"
)

func main() {
var i interface{}
//json.Unmarshal(byte(`["hi"]`), &i)
json.Unmarshal(byte(`{"a":"hi"}`), &i)

switch i.(type) {
case interface{}:
println("ARRAY")
case map[string]interface{}:
println("NOT ARRAY")
default:
fmt.Printf("%Tn", i)
}
}





share|improve this answer















You need to convert Filters type interface{} into the expected slice. If you don't really know what type it will be, you can use map[string]interface{}. Therefore change your Filters type to map[string]interface{}.



Per more information
If Filters can be an array (or not), then you might consider a type switch:



A simple example:



package main

import (
"encoding/json"
"fmt"
)

func main() {
var i interface{}
//json.Unmarshal(byte(`["hi"]`), &i)
json.Unmarshal(byte(`{"a":"hi"}`), &i)

switch i.(type) {
case interface{}:
println("ARRAY")
case map[string]interface{}:
println("NOT ARRAY")
default:
fmt.Printf("%Tn", i)
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 17:52

























answered Nov 16 '18 at 17:44









poypoy

6,59263465




6,59263465













  • If I use map[string]interface{} then I cannot assign array to Filters. (Filters can be array or map)

    – Shashank Sachan
    Nov 16 '18 at 17:47











  • I see. I'll update my answer.

    – poy
    Nov 16 '18 at 17:52











  • Thanks! This is going to work for me.

    – Shashank Sachan
    Nov 16 '18 at 18:58











  • Awesome! I'm glad I could help.

    – poy
    Nov 16 '18 at 19:00



















  • If I use map[string]interface{} then I cannot assign array to Filters. (Filters can be array or map)

    – Shashank Sachan
    Nov 16 '18 at 17:47











  • I see. I'll update my answer.

    – poy
    Nov 16 '18 at 17:52











  • Thanks! This is going to work for me.

    – Shashank Sachan
    Nov 16 '18 at 18:58











  • Awesome! I'm glad I could help.

    – poy
    Nov 16 '18 at 19:00

















If I use map[string]interface{} then I cannot assign array to Filters. (Filters can be array or map)

– Shashank Sachan
Nov 16 '18 at 17:47





If I use map[string]interface{} then I cannot assign array to Filters. (Filters can be array or map)

– Shashank Sachan
Nov 16 '18 at 17:47













I see. I'll update my answer.

– poy
Nov 16 '18 at 17:52





I see. I'll update my answer.

– poy
Nov 16 '18 at 17:52













Thanks! This is going to work for me.

– Shashank Sachan
Nov 16 '18 at 18:58





Thanks! This is going to work for me.

– Shashank Sachan
Nov 16 '18 at 18:58













Awesome! I'm glad I could help.

– poy
Nov 16 '18 at 19:00





Awesome! I'm glad I could help.

– poy
Nov 16 '18 at 19:00




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342831%2fgolang-redis-map-slice%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine