Parsing complex JSON using JTokens


  • Admin

    When you have to parse very nested and structured JSON, many times you will find out that the vanilla option that OpenBullet comes with is not enough and does not lead to the results you want.

    Luckily, there is a way better method to navigate a JSON object.

    Let's assume your website replied with this object

    {
      "menu": {
        "id": "file",
        "array": ["cat", "dog", "duck"],
        "value": "File",
        "popup": {
          "menuitem": [
            {"value": "New", "onclick": "CreateNewDoc()"},
            {"value": "Open", "onclick": "OpenDoc()"},
            {"value": "Close", "onclick": "CloseDoc()"}
          ]
        }
      }
    }
    

    You will have to enable JToken parsing and input the JToken in the Field Name box

    1f4b1029-711c-42a4-af21-8c3055e0a655-image.png

    There is a very nice website that can help you visualize the JSON object structure, especially if it's not indented but it's just a single-line very long string.
    http://jsonviewer.stack.hu/
    Just paste your text in the Text tab and then switch to the Viewer tab in order to see the tree. Every time you want to go in a deeper branch of the tree you have to use the . character, which selects a child from a parent node of the tree.
    I know, explained like this it sounds hard but just bear with me because it's actually really easy!

    Now let's assume you want to parse different things.

    Parsing the id
    We start from the root of the object tree and the first item we find is menu. Its children are id, array, value and popup.
    In this case we can simply write menu.id because the id is not an array.

    Parsing the array
    In this case, the child array is in fact an array of values, so we have 2 options.

    1. Parsing a specific value menu.array[1]
    2. Parsing all of the values menu.array[*] (you will have to enable Recursive in the parse block)

    Parsing the menu item values
    In this case the fields are nested, so we start from the root and we do menu.popup to get to the popup object, then we choose its child called menuitem which is an array, we choose all the values from it using menuitem[*] and then for each of those we choose the field called value.
    By putting everything together we get the following token menu.popup.menuitem[*].value

    NOTE: In order for this approach to work, your JSON object MUST start and end with curly brackets.
    If for example the website just returned you an array [1, 2, 3] you can just type for example [0].name to access the name field of the first element or [*].name to access the name field of every element of the array (using arrays directly without wrapping them in an object is only supported on the latest version of OpenBullet).

    If you're on an older version without JArray support you can do something like this to wrap the array with an object:
    {"array": <SOURCE>}
    which will become
    {"array": [1, 2, 3]}
    and then you can easily navigate it using the token array[*]



  • Roerei n° 1



  • Thanks !
    very useful



  • t h a n k s !


Log in to reply