New JSON parser

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?

You cannot iterate through a JSON dictionary, but you can access any element by key: json.at("1636896829")

my problem is that I don’t know the keys before I parse the JSON…

Ah – that’s unusual. I’ll look into a json.keys operator.

1 Like

this should help for the top level:

var jsonFixedForTBX = jsonData.replace("^\{(.*)\}$","\[$1\]");

P.S.: No it will not help with my problem - now I get a mix of object and array: [“value1”:{},“value2”:{}…]…
hmmm…

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:

Given the following JSON text:

{“Terms”:[
{ “tName”: “a”,
“tDef” : “a first def”,
“tID” : 1234,
“pID” : 567,
“pName”: “parent 1”
},
{
“tID”: 11,
“tName”: “b”,
“tDef” : “a second def”
}
]
}

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:

{“Terms”:[
{ “tName”: “a”,
“tDef” : “a first def”,
“tID” : 1234,
“pID” : 567,
“pName”: “parent 1”
“child”: { “tName”:“aa”, “tDef”:"a child def:}
},
{
“tID”: 11,
“tName”: “b”,
“tDef” : “a second def”
}
]
}

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…

Thanks,

dan

I’m treating a missing key in a dictionary as an error condition. Perhaps that’s wrong, and we should return the empty string.

I don’t see an obstacle to using try{} inside an each. Outside won’t work, because .each doesn’t return a value.

I think the dictionary nested inside a dictionary value is hitting a known issue. Working on that.

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?

Thanks…

I should also mention that I’ve experimented with typing the set variables but that didn’t seem to change behavior…

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.