How do I parse JSON Objects and return its values?

1 favourites
  • 7 posts
From the Asset Store
Welcome! I-Spy (Hidden objects) is an educational puzzle that is more than just seek-and-find activities.
  • Hello!

    I use the AJAX events to get this and similar Objects from a server:

    [
     {
     "id": "3510d1-43e-4c45-be00-156ffk6d5da",
     "name": "BWL",
     "description": "",
     "dateFrom": "2020-01-30T18:00:00.0000000Z",
     "dateTo": "2020-01-30T22:00:00.0000000Z",
     "raidMembers": [
     {
     "characterId": "3z3z4a-5364-473c-97b1-gf63kl06",
     "isConfirmed": false,
     "name": "Jorgen",
     "class": "Hunter",
     "role": "RangedDamage",
     "talents": "MM",
     "race": "Troll",
     "guild": "Bla Bla",
     "level": 24,
     "isMain": false,
     "confirmedAt": "2020-01-26T23:31:33.32299Z",
     "confirmedBy": "Jeff",
     "registeredAt": "2020-01-26T23:30:59.5301Z",
     "unregisteredAt": "2020-01-27T15:58:20.61561Z"
     },
     {
     "characterId": "ashh2a9-33ac-654-923f3-456kgh8bb",
     "isConfirmed": true,
     "name": "Jeff",
     "class": "Warrior",
     "role": "Tank",
     "talents": "Deep prot",
     "race": "Undead",
     "guild": "Bla Bla",
     "level": 60,
     "isMain": false,
     "confirmedAt": "2020-01-29T11:33:24.56272Z",
     "confirmedBy": "",
     "registeredAt": "2020-01-26T23:30:55.754507Z",
     "unregisteredAt": null
     },
     {
     "characterId": "d637hd-e256e-26d2-237-a8htz6hb15",
     "isConfirmed": true,
     "name": "Gubbo",
     "class": "Shaman",
     "role": "Healer",
     "talents": "Resto / Support",
     "race": "Orc",
     "guild": "Bla Bla",
     "level": 60,
     "isMain": false,
     "confirmedAt": "2020-01-29T11:33:24.526091Z",
     "confirmedBy": "",
     "registeredAt": "2020-01-27T11:22:03.78456Z",
     "unregisteredAt": null
     },
     {
     "characterId": "daafe672-833e-6270-3gb-f970h29e39",
     "isConfirmed": false,
     "name": "Gimp",
     "class": "Warrior",
     "role": "Tank",
     "talents": "Furry-Prot",
     "race": "Orc",
     "guild": "Bla Bla",
     "level": 60,
     "isMain": false,
     "confirmedAt": null,
     "confirmedBy": null,
     "registeredAt": "2020-01-27T17:03:19.3627Z",
     "unregisteredAt": "2020-01-27T17:03:20.389363Z"
     },
     {
     "characterId": "602j4e-43j89-d651-dag4a-jkh20ua38f",
     "isConfirmed": false,
     "name": "Looky",
     "class": "Rogue",
     "role": "MeleeDamage",
     "talents": "(°O°)",
     "race": "Orc",
     "guild": "Hamlo",
     "level": 60,
     "isMain": false,
     "confirmedAt": null,
     "confirmedBy": null,
     "registeredAt": "2020-01-28T03:53:18.35210Z",
     "unregisteredAt": "2020-01-28T03:53:19.747Z"
     },
     {
     "characterId": "056e716-ajk7-33fb-32fg-dgfd70s6",
     "isConfirmed": false,
     "name": "Sery",
     "class": "Priest",
     "role": "RangedDamage",
     "talents": "Shadow",
     "race": "Tauren",
     "guild": "Bla Bla",
     "level": 60,
     "isMain": false,
     "confirmedAt": null,
     "confirmedBy": null,
     "registeredAt": "2020-01-29T11:35:21.1520Z",
     "unregisteredAt": "2020-01-29T11:35:21.2555589Z"
     },
     {
     "characterId": "d956614d0-b775-75h4-bc5f-0956971dd",
     "isConfirmed": false,
     "name": "Myth",
     "class": "Priest",
     "role": "Healer",
     "talents": "CH 21/30",
     "race": "Troll",
     "guild": "Bla Bla",
     "level": 60,
     "isMain": false,
     "confirmedAt": "2020-01-29T14:14:54.23564Z",
     "confirmedBy": "",
     "registeredAt": "2020-01-29T13:59:56.0963920Z",
     "unregisteredAt": "2020-01-29T21:08:47.3569952Z"
     }
     ],
     "raidGroupId": "43gga-9453-4hgc-83d0-j3i927f"
     },
     {
     "id": "g3j6n2-2n5-442b-4c-23a0a6262hkl38",
     "name": "Molten Core",
     "description": "",
     "dateFrom": "2020-01-30T17:30:00.0000000Z",
     "dateTo": "2020-01-30T21:00:00.0000000Z",
     "raidMembers": [
     {
     "characterId": "jdfa9-3ffjc-f4-9cfd83-4j5kbb",
     "isConfirmed": false,
     "name": "Jeff",
     "class": "Warrior",
     "role": "Tank",
     "talents": "Deep prot",
     "race": "Undead",
     "guild": "Bla Bla",
     "level": 60,
     "isMain": false,
     "confirmedAt": "2020-01-26T23:32:57.36435Z",
     "confirmedBy": "Jeff",
     "registeredAt": "2020-01-27T18:04:08.7722Z",
     "unregisteredAt": "2020-01-30T17:44:13.72376Z"
     },
     {
     "characterId": "798a7g-8f3gh-fd55h-dfg-d8g2h98j6",
     "isConfirmed": false,
     "name": "Jorgen",
     "class": "Hunter",
     "role": "RangedDamage",
     "talents": "MM",
     "race": "Troll",
     "guild": "Bla Bla",
     "level": 24,
     "isMain": false,
     "confirmedAt": "2020-01-26T23:32:48.45668Z",
     "confirmedBy": "Jeff",
     "registeredAt": "2020-01-26T23:32:52.64566Z",
     "unregisteredAt": "2020-01-30T17:44:10.456453Z"
     },
     {
     "characterId": "5g34e-7gg-3451-b534a-df98gjf",
     "isConfirmed": false,
     "name": "Looky",
     "class": "Rogue",
     "role": "MeleeDamage",
     "talents": "(°O°)",
     "race": "Orc",
     "guild": "Hamlon",
     "level": 100,
     "isMain": false,
     "confirmedAt": null,
     "confirmedBy": null,
     "registeredAt": "2020-01-28T03:54:01.54533Z",
     "unregisteredAt": null
     },
     {
     "characterId": "98d7fg6-adf77-4gdb-bhr2-d1kjoij5a6",
     "isConfirmed": false,
     "name": "Seri",
     "class": "Priest",
     "role": "RangedDamage",
     "talents": "Shadow",
     "race": "Tauren",
     "guild": "Bla Bla",
     "level": 60,
     "isMain": false,
     "confirmedAt": null,
     "confirmedBy": null,
     "registeredAt": "2020-01-29T11:35:41.6438Z",
     "unregisteredAt": null
     }
     ],
     "raidGroupId": "72828ha-4233-4f4c-8230-asd987fb"
     }
    ]
    

    I then use JSON to parse it.

    But this is unpractical because I want to be able to re-referance the Data at a later time and I need to know how I can just pick specific keys/values.

    I stored the parsed JSON value in a global variable. But I guess this is wrong for easily accessing specific object data later.

    1) Can someone please give me an example how I can write .name of .raidmembers with a text object?

    2) And how can I reference .name if there are multiple members / nested objects?

    raids.raidmembers.name[0]

    raids.raidmembers.name[1]

    raids.raidmembers.name[2]

    Something like that?

    3) Is there a way to use Javascript Objects within the event system?

    Thank you in advance! :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • See: construct.net/en/make-games/manuals/construct-3/plugin-reference/json

    Look at the section 'Arrays'.

    Here's how you address arrays:

    Like most of Construct JSON arrays use zero-based indices, so the path array.0 refers to the first element (123) and array.1 refers to the second element (456), and so on.

    Does that help?

  • Thanks for the reply. I know about arrays. But how would I know the array index from, lets say the player name and his level inside one of the raids/dungeons? I would need arrays with named indices. Instead of array.0 I would need something like array.raidID.playerID.name. Or array["raidID","playerID","name"].

  • Do you have to use that JSON structure in your first post? If I was doing that and was focused on using a particular key as an index, I would put that key as a top-level identifier for easy access.

    E.g.:

    "Jorgen" : 
     {
     "characterId": "3z3z4a-5364-473c-97b1-gf63kl06",
     "isConfirmed": false,
     "name": "Jorgen",
     "class": "Hunter",
     "role": "RangedDamage",
     "talents": "MM",
     "race": "Troll",
     "guild": "Bla Bla",
     "level": 24,
     "isMain": false,
     "confirmedAt": "2020-01-26T23:31:33.32299Z",
     "confirmedBy": "Jeff",
     "registeredAt": "2020-01-26T23:30:59.5301Z",
     "unregisteredAt": "2020-01-27T15:58:20.61561Z"
     },
    
    

    If that doesn't work (e.g. you want to use multiple keys), I think I would add a C3 function like FindIndex(jsonUID, path,key,matchValue) which references a JSON object, goes to a path loops through the objects at that level, checks if the object has the key, if it does, check if that keys value matches the matchValue, if it does return the index of the object.

  • Thank you very much for this idea.

    I asked the programmer if he could restructure it that way and he told me it wouldn't work, because the IDs are dynamic.

    But he told me about variable.filter(). That is pretty handy and I think I can use it.

    Thanks again. :)

  • To your second question in your original post :

    If, say, you want to know the name of the third member of the first raid you'd use a path like so :

    raids.0.raidMembers.2.name

    If you want to access raid members data by ID, I would suggest you used some sort of helper function.

    Like so : drive.google.com/file/d/1o5Ndw6T_vsmWHeTZA-rCAJTjmfsBiXhq/view

    The function can either return all data as a small JSON or a particular key provided a raid id, member id and an optional key name.

  • Wow this is super awesome! I will test it out as soon as I am at home. Thank you for the efforts! :)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)