JSON Parser getCurrentToken()

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



JSON Parser getCurrentToken()



I have a JSON string that I want to parse out. I have use the below code many times but for whatever reason this time it is not working. Can anyone poke any holes in this logic? Or point out why this may not be the best way to parse through a string?



String tokencred = ["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]


String tokencred = ["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]


JSONParser parser = JSON.createParser(tokencred);
//parse and map the returned string
while (parser.nextToken() != null)
String parsedId;
String parsedToken;
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Id'))
parser.nextToken();
parsedId = parser.getText();

if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Token'))
parser.nextToken();
parsedToken = parser.getText();

tokenCredMap.put(parsedId,parsedToken);

system.debug('tokenCredMap:: ' + tokenCredMap);

14:31:15.0 (808193208)|USER_DEBUG|[153]|DEBUG|tokenCredMap:: null=null, 003m000000yKLySkkL=null



I do need the map as there is additional logic that will enable me to have multiple token/id combos.



UPDATE:


15:02:55.11 (726597242)|USER_DEBUG|[314]|DEBUG|this is the updated value ["Id":"00Qm0000009qqdiEAA","Token":"J5h8yz004mFJi44ozuhw"]
15:02:55.11 (727270334)|USER_DEBUG|[323]|DEBUG|fieldName: Id
15:02:55.11 (727412370)|USER_DEBUG|[323]|DEBUG|fieldName: Token
15:02:55.11 (727597218)|USER_DEBUG|[342]|DEBUG|tokenCredMap:: null=null, 00Qm0000009qqdiEAA=null
15:02:55.11 (732474914)|DML_BEGIN|[360]|Op:Update|Type:Lead|Rows:1




2 Answers
2



I think the issue is that you are not advancing the parser after saving the ParsedId. Let's say that the if statement for the Id is evaluated as true. You then save the ParsedId, but you are not advancing the parser forward afterwords. That means the second If statement can never be true because the parser is still pointing to the value of the Id.





This is the most direct answer to the question.
– gNerb
32 mins ago





Yep I agree with gNerb about that.
– Keith C
28 mins ago




If would write that like this:


Map<String, String> tokenCredMap = new Map<String, String>();
String tokencred = '["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]';

List<Object> items = (List<Object>) JSON.deserializeUntyped(tokencred);

for (object item : items)
Map<String, Object> typedItem = (Map<String, Object>) item;
tokenCredMap.put((Id) typedItem.get('Id'), (String) typedItem.get('Token'));

system.debug('tokenCredMap:: ' + tokenCredMap);



as it leaves working at the token level to the deserializeUntyped method and so avoids the risk of bugs in that area.


deserializeUntyped



(In your code often both parsedId and parsedToken will be null and if other attributes were in the JSON your code would be tripped up. Also in JSON the order of the attributes is meant to be open and your logic requires the attributes to be in a specific order.)


parsedId


parsedToken





This is the best answer (posted so far) although it doesn't explain why what the OP is doing doesn't work. I was in the process of writing an answer that combined both of the answers that were just posted but figured not much point anymore :P)
– gNerb
31 mins ago





@gNerb Yeah I think Hans is describing the core problem and I'm just commenting how fragile in general a hand-written parser is.
– Keith C
29 mins ago





this is great. thank you for posting a different look at parsing. Unfortunately it is still not parsing out the token for what ever reason. What confuses me is: if there was another process that was rolling it back then the ID wouldn't be saved to the map, right? I will update my post with the results from your solution.
– Olivia
29 mins ago





@Olivia The best way to develop logic like this is to use a unit test so you can debug a simple case.
– Keith C
26 mins ago





Keith I had a hard time getting your sample to work due to some run time type conversion errors. I updated your code with a working sample. Hope you don't mind.
– gNerb
21 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS