Jump to content

Recommended Posts

Posted

Hi, I'm hoping someone can help me with this JSON problem.

I can manage to extract some JSON data but I'm not knowledgable enough to figure out the syntax to extract the other values that have randomly generated key labels and the data that follows afterwards.

In my attempts, I was trying to get the random key values from  .History[' & 0 & '].Given.Currency  but everything I tried just returned a blank, no data.

Strangely (to me at least) the following didn't get any data either when using the exact random key:
.History[' & 0 & '].Given.Currency.1755f59b3e6849db4171f93c524db28f.id

Would appreciate any help. Thanks in advance.

snippet example of code:

#include <json.au3>

$object=json_decode($historyData)

$epochTime = json_get($object, '.History[' & 0 & '].Timestamp')
$otherUserId = json_get($object, '.History[' & 0 & '].OtherUserId')

snippet example of JSON data:

{
    "History": [
        {
            "OtherUserId": 10001,
            "Given": {
                "Currency": {
                    "1755f59b3e6849db4171f93c524db28f": {
                        "amount": 2480,
                        "id": "Coins"
                    },
                    "6e7780015fbea9480b95be70623c4103": {
                        "amount": 20,
                        "id": "Coins"
                    }
                }
            },
            "Timestamp": 1735872629,
            "Received": {
                "Power": {
                    "b3b4c54ef7461a9941dff8e577513bf8": {
                        "amount": 3,
                        "id": "Jump"
                    }
                }
            }
        }
    ]
}

 

  • Solution
Posted (edited)
#include <json.au3> ; https://github.com/Sylvan86/autoit-json-udf
$object=_JSON_Parse(FileRead(StringTrimRight(@ScriptFullPath, 4) & ".json"))
ConsoleWrite('- >' &  _JSON_Get($object, '.History[0].Timestamp') & '<' & @CRLF) ; 1735872629
ConsoleWrite('- >' &  _JSON_Get($object, '.History[0].OtherUserId') & '<' & @CRLF) ; 10001
ConsoleWrite('- >' &  _JSON_Get($object, '.History[0].Given.Currency.1755f59b3e6849db4171f93c524db28f.amount') & '<' & @CRLF) ; 2480

..or..

#include <json.au3> ; https://github.com/Sylvan86/autoit-json-udf
$object=_JSON_Parse(FileRead(StringTrimRight(@ScriptFullPath, 4) & ".json"))
$iIndex = 0
While _JSON_Get($object, '.History[' & $iIndex & '].Timestamp')
    ConsoleWrite('- >' &  _JSON_Get($object, '.History[' & $iIndex & '].Timestamp') & '<' & @CRLF)
    ConsoleWrite('- >' &  _JSON_Get($object, '.History[' & $iIndex & '].OtherUserId') & '<' & @CRLF)
    $mMap = _JSON_Get($object, '.History[' & $iIndex & '].Given.Currency')
    $aMapKeys = MapKeys($mMap)
    For $n = 0 To UBound($aMapKeys) -1
        ConsoleWrite('- ' & $n & ' - >' &  _JSON_Get($object, '.History[' & $iIndex & '].Given.Currency.' & $aMapKeys[$n] & '.amount') & '<' & @CRLF)
        ConsoleWrite('- ' & $n & ' - >' &  _JSON_Get($object, '.History[' & $iIndex & '].Given.Currency.' & $aMapKeys[$n] & '.id') & '<' & @CRLF)
    Next
    $iIndex += 1
WEnd

using https://github.com/Sylvan86/autoit-json-udf

..also, remember to read the forum rules ;)

Edited by argumentum
better ?

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

Thanks for posting a possible solution.

Unfortunately I got a few  error: MapKeys(): undefined function  messages. I'll play around with it and see if I can get something working.

Didn't realise about the rule against gaming related stuff, albeit in this instance its about policing trades rather that exploiting, which I imagine is where the rule is targeting.

If all else fails, might just be as quick to write up some code to extract it using a different method, even if less efficient.

Thanks again.

Posted

Ah, you're right, I was using version: 3.3.14.5, but have now installed the new version: 3.3.16.1 and the example code posted above works now.

The 2nd example code above is what I was looking for, I'm going to try using that.

Thanks argumentum for the example code.

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...