-1

I have following example json:

{
    "someObjects": [
    {
        "value1": "something here"
    },
    {
        "value1": "something else"
    }
    ]
}

I want to count the amount of objects inside the "someObjects" array in golang without using any lib.

The json is there in a string and I do not have the corresponding struct lying around (since it is actually pretty big). How can I count this?

4
  • 1
    Without any lib? Go is not like JavaScript, it doesn't have native JSON support. If you don't want to use libs you'll have to parse the JSON yourself. Commented Sep 22, 2023 at 16:59
  • 1
    This is done by a bit of programming. What have you tried? Where did you get stuck? Why?. Note that the Go stdlib has tools to unmarshal JSON. Using a clever adhoc data structure might be a sensible first approach in which case this is some 20 lines of simple code. Commented Sep 22, 2023 at 17:51
  • you can use the reflection standard package. Try to read more about it in this link this package lets you to manipulate objects with the arbitrary type interface{} (in recents versions now is called any type). Remember an important thing, golang is a strictly static typed language so the most easy way to do this is over an struct created by yourself and pass it to any of the functions in the json package that lets you unmarshal json's in structs Commented Sep 22, 2023 at 23:08
  • Does this answer your question? golang json unmarshal part of map[string]interface{} Commented Sep 23, 2023 at 16:42

1 Answer 1

0

Not even standard library ? Then you have to parse whatever coming from the network yourself which you can do using reflections in Go. But even in that case you have to use "reflect" package.

Sign up to request clarification or add additional context in comments.

2 Comments

Of course I meant the standard lib is fine. Im sorry.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.