2

am having trouble understanding how https://github.com/buger/jsonparser , works. I dont feel the examples given are easy to understand, can someone help me a with good example for each of methods under the jsonparser package ? Am specifically looking for a way to make "jsonparser.EachKey" work, i have this schema, and i need to fetch all the keys under "attributes"

{
    "provider": {
    "version": 0,
    "block": {
      "attributes": {
        "access_approval_custom_endpoint": {
            "type": "string",
            "description_kind": "plain",
            "optional": true
          },
        "access_approval_adhoc": {
            "type": "string",
            "description_kind": "plain",
            "optional": true
          }
      }
    }
}
}

So that the output is a list of keys under attributes : ["access_approval_custom_endpoint","access_approval_adhoc"]

And the important thing is I need to use only this buger/jsonparser , i cant use anything else. Can someone help me with some code to achieve this ?

  • Hello, welcome to StackOverflow. The doc for any publicly available go package can be found on https://pkg.go.dev/ . For jsonparser : https://pkg.go.dev/github.com/buger/jsonparser . For this package, the doc has a Reference section with an example for each of tits function. The example for `EachKey` can be found here : https://pkg.go.dev/github.com/buger/jsonparser#readme-eachkey – LeGEC Sep 30 '22 at 12:28
  • yea, i read the doc, but am unable to retrieve the keys. Its only iterating over eachkey and storing that keys value in the "value" argument. what am looking for is to fetch the keys, like in the eg i gave above. @LeGEC – Mohammed Saaduddin Ansari Oct 01 '22 at 18:36
  • That would be [`ObjectEach`](https://pkg.go.dev/github.com/buger/jsonparser#readme-objecteach) – LeGEC Oct 01 '22 at 18:55

1 Answers1

1

As described in the documentation : EachKey actually requires you to know the keys beforehand, and provide them to the function.

If you want to iterate on an object and see all the keys it contains (without knowing the keys), ObjectEach will be more appropriate.

LeGEC
  • 46,477
  • 5
  • 57
  • 104