If I take this JSON sample and try to process it with TBX 9.1:
var myData = {"1636896829":{"name":"Test Note No. 2","text":"Sed ut perspiciatis unde omnis öäü"},"1637255662":{"name":"Test Note No. 1","text":"Ea aute consectetur non velit."},"1638016419":{"name":"Test Note No. 3","text":"Anim nostrud aliqua quis."}}
How can I iterate through the found data - it’s no array on the top level but a list of objects and so myData.json.each(x) will not do the job - neither will myData.json[0] deliver the node?
I’ve also been trying to work out some of the new 9.1 features - especially the new stream operations with JSON. I’m trying to understand the rules and limitations to parsing with the new functions. A few points of confusion for me:
I have been able to parse this into new notes and attributes - but there seem to be some edge cases that are tricky. For instance,
If, a term doesn’t exist (such as pName in the second element) and the value is of type string we seem to return “no such field”. If the value is a number (such as in the case of the missing pID), then a 0 is returned. Is this the expected behavior?
I experimented with trying to use the new .try function - but not quite sure if it can be combined with the .each operator…
I was also looking to see if there was a way to handle nesting such as:
But the nesting of child really seems to confuse things…I’m sure I’m missing something - if folk have good examples or explanations to share it would be really helpful…
As I have been experimenting with parsing JSON further, I am trying to figure out how to parse sets embedded in a JSON object… for example, given the following snippet:
"Terms": [{
"lastModified": "2022-01-11T06:49:27-06:00",
"nID": 1638545471,
"ShortTitle": "Emissions Inventory Scopes",
"parentName": "GlossaryTerms",
"pID": 1635756386,
"summary": "This is a summary of the term emissions inventory scopes",
"abbreviation": "scope",
"examples": "This is an example",
"attribution": "\"Common Knowledge\", \"GHG Protocol Corporate Standard\"",
"glossary": ["Prime"],
"category": ["Basic", "Fun"],
"usage": "There isn't any",
"description": "This is some text for Emissions Inventory Scopes\n",
"contexts": ["Dan"]
},
I have a number of sets that when I try to assign them to a variable with something like:
var vContexts=json["contexts"];
var vCategory=json["category"];
var vGlossary=json["glossary"];
var vExamples=json["examples"];
var vSummary=json["summary"];
var vSyn=json["synonyms"];
I get an “Out of Bounds” error in the value that I’ve not seen before… It is of course not unlikely that I have some kind of syntax error, and I continue to review my code and the file (which parses cleanly in JSON Lint) but are there recommended techniques for handling this?
Yes: we currently have trouble with dictionary values that are lists — here, lists of dictionaries. Send an example of the JSON object you’re working on to tinderbox@eastgate.com and we may have ideas.