tidwall / sjson

Set JSON values very quickly in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON-LD path with multiple @ keywords fails

fils opened this issue · comments

I trying to write a line where I update a context entry in a JSON-LD document. The schema.org context is often either https://schema.org or http://schema.org. This is some code to normalize them to a value.

In the code at https://github.com/gleanerio/gleaner/blob/df--dev/tools/contextChange/main.go line 118 I have

jsonld, err = sjson.Set(jsonld, tns, sdoc)

When this has a value for tns like:

@context.@vocab

It will not update the JSON document (they are in the data directory next to the go main file)

if it is

@context.schema

it entry is found and updated.

It seems is the second element have an @ keyword prefix (ie, the @vocab) cause sjon to not find the entry.

Any guidance is appreciated

It seems a \ will resolve it as in

if strings.HasPrefix(k, "@") {
		 tns = fmt.Sprintf("@context.\\%s", k)  
 } else {
	 	tns = fmt.Sprintf("@context.%s", k)  
 }

from https://github.com/gleanerio/gleaner/blob/48d4170c6d60ee5e051add39f5286c1ae93a2bd6/tools/contextChange/main.go#L121-L125

not sure if this is really the best way but seems to be holding up to testing..

Thanks for reporting. I figured out what the issue was and fixed it.

The gjson library (which sjson uses to find where to replace the new values from sjson.Set) did not allow for changing values for modifier path components, which start with the @ character. I made an update to gjson to check that that path component was an actual known modifier. 😅