0

Every time I resolve one issue I get into another similar but different.

I need to convert into struts below JSON, but some parts are dynamic. and I am not very experienced with this conversions and I get very confused.

Below I have two fields that are different not static ( I was able with the help of this forum resolve a similar issue with one dynamic entry, but now I have two). the fields I called them "This string changes" so is less confusing.

[null,null,"hub:zWXKROOM","presence_state",{"74ce1906-af89-48a9-aec7-501369509000":{"metas":[{"context":{"embed":false,"mobile":false},"permissions":{"close_hub":false,"embed_hub":false,"fly":true,"join_hub":true,"kick_users":false,"mute_users":false,"pin_objects":false,"spawn_and_move_media":true,"spawn_camera":true,"spawn_drawing":false,"spawn_emoji":true,"update_hub":false,"update_hub_promotion":false,"update_roles":false},"phx_ref":"tGJf9IxredI=","phx_ref_prev":"zJ3pFzeYafM=","presence":"room","profile":{"avatarId":"3IADk9x","displayName":"real changeling"},"roles":{"creator":false,"owner":false,"signed_in":false}}]},"774e91d5-a324-47d7-ba75-edf9ed5bbe1a":{"metas":[{"context":{"embed":false,"mobile":false},"permissions":{"close_hub":false,"embed_hub":false,"fly":true,"join_hub":true,"kick_users":false,"mute_users":false,"pin_objects":false,"spawn_and_move_media":true,"spawn_camera":true,"spawn_drawing":false,"spawn_emoji":true,"update_hub":false,"update_hub_promotion":false,"update_roles":false},"phx_ref":"NnAT0YpIaUg=","phx_ref_prev":"tzv+xV6h0Rs=","presence":"room","profile":{"avatarId":"PcJ8Sxb","displayName":"GoBotWebSockets"},"roles":{"creator":false,"owner":false,"signed_in":false}}]},"9bd22f70-521a-49c2-9cb9-ac58dabfa1d6":{"metas":[{"context":{"embed":false,"mobile":false},"permissions":{"close_hub":false,"embed_hub":false,"fly":true,"join_hub":true,"kick_users":false,"mute_users":false,"pin_objects":false,"spawn_and_move_media":true,"spawn_camera":true,"spawn_drawing":false,"spawn_emoji":true,"update_hub":false,"update_hub_promotion":false,"update_roles":false},"phx_ref":"kPknFXlNkMo=","phx_ref_prev":"IA2Es263VdA=","presence":"room","profile":{"avatarId":"3IADk9x","displayName":"killab33z"},"roles":{"creator":false,"owner":false,"signed_in":false}}]},"f87b718a-c873-40a9-99db-91b4d0f7f4de":{"metas":[{"context":{"embed":false,"mobile":false},"permissions":{"close_hub":true,"embed_hub":true,"fly":true,"join_hub":true,"kick_users":true,"mute_users":true,"pin_objects":true,"spawn_and_move_media":true,"spawn_camera":true,"spawn_drawing":true,"spawn_emoji":true,"update_hub":true,"update_hub_promotion":false,"update_roles":true},"phx_ref":"XCCt44iesAo=","presence":"lobby","profile":{"avatarId":"https://s3.amazonaws.com/readyplayerbaker/avatars_baked/89e86e1a-43c7-4520-8f91-9a94ed42a722.glb","displayName":"ReK2"},"roles":{"creator":true,"owner":true,"signed_in":true}}]}}]

with a lot of help of someone here I was able to convert a very similar one but with two I cant seem to get it to work. I keep getting empty results..

the similar approach is Here

I have tried replicating with a field above and other similar things. I have look many blogs for this situation, no luck.

I also have try this tools, but is not really working because the tool does not know the details

edit: here is from json lint:

[
  null,
  null,
  "hub:zWXKROOM",
  "presence_state",
  {
    "74ce1906-af89-48a9-aec7-501369509000": {
      "metas": [
        {
          "context": {
            "embed": false,
            "mobile": false
          },
          "permissions": {
            "close_hub": false,
            "embed_hub": false,
            "fly": true,
            "join_hub": true,
            "kick_users": false,
            "mute_users": false,
            "pin_objects": false,
            "spawn_and_move_media": true,
            "spawn_camera": true,
            "spawn_drawing": false,
            "spawn_emoji": true,
            "update_hub": false,
            "update_hub_promotion": false,
            "update_roles": false
          },
          "phx_ref": "tGJf9IxredI=",
          "phx_ref_prev": "zJ3pFzeYafM=",
          "presence": "room",
          "profile": {
            "avatarId": "3IADk9x",
            "displayName": "real changeling"
          },
          "roles": {
            "creator": false,
            "owner": false,
            "signed_in": false
          }
        }
      ]
    },
    "774e91d5-a324-47d7-ba75-edf9ed5bbe1a": {
      "metas": [
        {
          "context": {
            "embed": false,
            "mobile": false
          },
          "permissions": {
            "close_hub": false,
            "embed_hub": false,
            "fly": true,
            "join_hub": true,
            "kick_users": false,
            "mute_users": false,
            "pin_objects": false,
            "spawn_and_move_media": true,
            "spawn_camera": true,
            "spawn_drawing": false,
            "spawn_emoji": true,
            "update_hub": false,
            "update_hub_promotion": false,
            "update_roles": false
          },
          "phx_ref": "NnAT0YpIaUg=",
          "phx_ref_prev": "tzv+xV6h0Rs=",
          "presence": "room",
          "profile": {
            "avatarId": "PcJ8Sxb",
            "displayName": "GoBotWebSockets"
          },
          "roles": {
            "creator": false,
            "owner": false,
            "signed_in": false
          }
        }
      ]
    },
    "9bd22f70-521a-49c2-9cb9-ac58dabfa1d6": {
      "metas": [
        {
          "context": {
            "embed": false,
            "mobile": false
          },
          "permissions": {
            "close_hub": false,
            "embed_hub": false,
            "fly": true,
            "join_hub": true,
            "kick_users": false,
            "mute_users": false,
            "pin_objects": false,
            "spawn_and_move_media": true,
            "spawn_camera": true,
            "spawn_drawing": false,
            "spawn_emoji": true,
            "update_hub": false,
            "update_hub_promotion": false,
            "update_roles": false
          },
          "phx_ref": "kPknFXlNkMo=",
          "phx_ref_prev": "IA2Es263VdA=",
          "presence": "room",
          "profile": {
            "avatarId": "3IADk9x",
            "displayName": "killab33z"
          },
          "roles": {
            "creator": false,
            "owner": false,
            "signed_in": false
          }
        }
      ]
    },
    "f87b718a-c873-40a9-99db-91b4d0f7f4de": {
      "metas": [
        {
          "context": {
            "embed": false,
            "mobile": false
          },
          "permissions": {
            "close_hub": true,
            "embed_hub": true,
            "fly": true,
            "join_hub": true,
            "kick_users": true,
            "mute_users": true,
            "pin_objects": true,
            "spawn_and_move_media": true,
            "spawn_camera": true,
            "spawn_drawing": true,
            "spawn_emoji": true,
            "update_hub": true,
            "update_hub_promotion": false,
            "update_roles": true
          },
          "phx_ref": "XCCt44iesAo=",
          "presence": "lobby",
          "profile": {
            "avatarId": "https://s3.amazonaws.com/readyplayerbaker/avatars_baked/89e86e1a-43c7-4520-8f91-9a94ed42a722.glb",
            "displayName": "ReK2"
          },
          "roles": {
            "creator": true,
            "owner": true,
            "signed_in": true
          }
        }
      ]
    }
  }
]

[edit2] I did a lot of multiple tries but still is not printing anything at all! is like there is no data but there is data... can someone please check my code and see what im I doing wrong? this last one I did step by step with a tutorial, still is not even showing any data, I wish it did so at least I know what to change.. https://gitlab.com/rek2/gohubsbot/-/blob/master/websocketsListen.go thanks

rek2
  • 101
  • 12
  • 5
    The JSON in the question is not valid. Check and handle the error returned from json.Unmarshal. Use a map to represent JSON objects with dynamic keys per our [discussion a couple of days ago](https://stackoverflow.com/questions/62698268/whats-the-best-way-to-get-content-from-a-generic-and-somehow-dynamic-go-map/62698912?noredirect=1#comment110885380_62698912). –  Jul 05 '20 at 03:30
  • @iLoveReflection hello again :) the json is possible to not be valid, since I added the parts manually below but is an array, is similar to the one you helped me with, only that this time the above part has one array instead of Join/Leave see here the correct json https://app.quicktype.io?share=8kZqe0XZwrQhZQLYcZQF thanks. – rek2 Jul 05 '20 at 12:58
  • @iLoveReflection also forgot to add there is no error I just get empty result in my map/maps im creating to try to populate them. is very very similar to the one the other day, but instead of Join is a dynamic object array now as well. I put a link to the json on my answer above this one, Im trying to use the same code i had since is from the same data stream, just added a new struct and a new case check. – rek2 Jul 05 '20 at 15:45
  • The JSON in the question has duplicate object keys. I have no advice on how to handle that. In addition, there are syntax errors. –  Jul 05 '20 at 17:02
  • @iLoveReflection I updated the question in case you do not want to go to another website to see it. if that json is wrong there is a bigger problem not on my side with that websocket api from mozilla hubs – rek2 Jul 05 '20 at 17:21
  • @iLoveReflection I just passed the json on a json lint tool and says is 100% valid... what are you talking about? you going to get confused possible people that will help, that read the comments and move on because of your comment with wrong json even do I had a link to a json site with the correct one and later I edited the questions :( – rek2 Jul 06 '20 at 14:39
  • I updated the question more with my last changes, that I did following a advance tutorial: did a lot of multiple tries but still is not printing anything at all! is like there is no data but there is data... can someone please check my code and see what im I doing wrong? this last one I did step by step with a tutorial, still is not even showing any data, I wish it did so at least I know what to change.. https://gitlab.com/rek2/gohubsbot/-/blob/master/websocketsListen.go thanks – rek2 Jul 06 '20 at 17:36

1 Answers1

0

ok after a lot of debugging, reading, reading, hitting my head and stripping json to make it simples to post on json to go tools... I finally got it.. the main issue?

//the type needs to be a map itself
type PresenceState map[string]State

Also:

// even do I did try  this above I never called it the right way before so was giving up and moving on... but I have to use a pointer

for k, o := range *v {}

is working now. I am getting the keys and objets when there is a presence state at login or change so I can map id to usernames

rek2
  • 101
  • 12