Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8896051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:56:40+00:00 2026-06-14T23:56:40+00:00

As long as I have key-value pairs unmarshalling is pretty straight forward but how

  • 0

As long as I have key-value pairs unmarshalling is pretty straight forward but how would I unmarshall an array of different types in different order? The single elements are well defined and known but the order is not.

I can not come up with a beautiful solution.

Would I try and error over all elements?
Is there some kind of union type that could do that for me?

playground version

package main

import (
    "encoding/json"
    "fmt"
)

var my_json string = `{
    "an_array":[
        "with_a string",
        {
            "and":"some_more",
            "different":["nested", "types"]
        }
    ]
}`

type MyInner struct {
    And     string
    Different   []string
}

type MyJSON struct {
    An_array []json.RawMessage
}

func main() {
    var my_json_test MyJSON

    e := json.Unmarshal([]byte(my_json), &my_json_test)
    if e != nil {
        fmt.Println(e)
    } else {
        for index, value := range my_json_test.An_array {
            fmt.Println("index: ", index)
            fmt.Println("value: ", string(value))
        }
        var my_inner MyInner
        err := json.Unmarshal(my_json_test.An_array[1], &my_inner)
        if err != nil {
            fmt.Println(err)
        } else {
            fmt.Println("inner structure: ", my_inner)
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:56:42+00:00Added an answer on June 14, 2026 at 11:56 pm

    Go official blog has a nice article about encoding/json: JSON and GO. It’s possible to “Decode arbitrary data” into an interface{} and use type assertion to determine the type dynamically.

    Your code can be probably modified to this:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    var my_json string = `{
        "an_array":[
        "with_a string",
        {
            "and":"some_more",
            "different":["nested", "types"]
        }
        ]
    }`
    
    func WTHisThisJSON(f interface{}) {
        switch vf := f.(type) {
        case map[string]interface{}:
            fmt.Println("is a map:")
            for k, v := range vf {
                switch vv := v.(type) {
                case string:
                    fmt.Printf("%v: is string - %q\n", k, vv)
                case int:
                    fmt.Printf("%v: is int - %q\n", k, vv)
                default:
                    fmt.Printf("%v: ", k)
                    WTHisThisJSON(v)
                }
    
            }
        case []interface{}:
            fmt.Println("is an array:")
            for k, v := range vf {
                switch vv := v.(type) {
                case string:
                    fmt.Printf("%v: is string - %q\n", k, vv)
                case int:
                    fmt.Printf("%v: is int - %q\n", k, vv)
                default:
                    fmt.Printf("%v: ", k)
                    WTHisThisJSON(v)
                }
    
            }
        }
    }
    
    func main() {
    
        fmt.Println("JSON:\n", my_json, "\n")
    
        var f interface{}
        err := json.Unmarshal([]byte(my_json), &f)
        if err != nil {
            fmt.Println(err)
        } else {
            fmt.Printf("JSON: ")
            WTHisThisJSON(f)
        }
    }
    

    It gives output as follows:

    JSON:
     {
        "an_array":[
        "with_a string",
        {
            "and":"some_more",
            "different":["nested", "types"]
        }
        ]
    } 
    
    JSON: is a map:
    an_array: is an array:
    0: is string - "with_a string"
    1: is a map:
    and: is string - "some_more"
    different: is an array:
    0: is string - "nested"
    1: is string - "types"
    

    It’s not complete yet, but shows how it’s gonna work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With dictionary I mean an array of key / value pairs with unique keys.
I have a Set of keys and a List of key/value pairs. The values
A bunch of key/value pairs, from an object that may have duplicate keys, need
Say I have a population of key-value pairs which I plan to store in
I know there are three different, popular types of non-sql databases. Key/Value: Redis, Tokyo
I am using a regular expression to extract key-value pairs from arbitrarily long input
I define a base class which have a Long primary key , just like
Currently I have Long Pres Gesture Recognizers on four different TableViews (two in each
I need to get key/value pairs from XML to populate member info on a
Here's what I'm trying to do: - Retrieve a Map of key-value pairs from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.