Skip to main content
Solved

Creation of nested (multilevel) JSON


albinepro
Supporter
Forum|alt.badge.img+7

I want to create multi-level JSON (nested) with a specific attribution. I have followed this article. I need to get this structure:

Following the tutorial I'm not able to add more attributes to the top layer as is written also in JSONTemplater help. I need to get something like this:

[{
	"category": "kategorie1",
    "caption": "A",
	"features" : [
      {
         "name" : "A1",
         "rotation" : "0",
         "bbox" : [ -733690, -1024463, -732733, -1023916 ]
      },
	        {
         "name" : "A2",
         "rotation" : "0",
         "bbox" : [ -733093, -1024114, -732939, -1023902 ]
      },
		...
   ]
},
{
   "category": "kategorie2",
   "caption": "B",
   "features" : [
      {
         "name" : "B1",
         "rotation" : "0",
         "bbox" : [ -732924, -1023936, -732707, -1023601 ]
      },
      {
         "name" : "B2",
         "rotation" : "0",
         "bbox" : [ -733058, -1023918, -732884, -1023584 ]
      },
		...
	]
}
]

All I can get so far is this:

{
   "A" : [
      {
         "name" : "A1",
         "rotation" : "0",
         "bbox" : "-733690""-1024463""-732733""-1023916" ]
      },
      {
         "name" : "A2",
         "rotation" : "0",
         "bbox" : [ -733093, -1024114, -732939, -1023902 ]
      },
      ...
   ],
   "B" : [
      {
         "name" : "B1",
         "rotation" : "0",
         "bbox" : [ -732924, -1023936, -732707, -1023601 ]
      },
      {
         "name" : "B2",
         "rotation" : "0",
         "bbox" : [ -733058, -1023918, -732884, -1023584 ]
      },
      ...
   ]
}

My Templater setup is as follows:

ROOT

{| 
    fme:process-features("OBLAST")
|}

OBLAST sub-template

{
       fme:get-attribute("pismeno") : [
        fme:process-features("PRVKY""pismeno", fme:get-attribute("pismeno"))
        ]
}

PRVKY sub-template

{
                "name": fme:get-attribute("CISLO"),
				"rotation":"0",
                "bbox": [fme:get-attribute("_xmin"),fme:get-attribute("_ymin"),fme:get-attribute("_xmax"),fme:get-attribute("_ymax")]
                
            }

I would need to have the top level as separated object with more key-value pairs (attributes) as shown above - like in the last level features. If I try to add keys as in PRVKY sub-template to the OBLAST sub-template I get parsing error with invalid syntax.

Can someone guide me, please? I attach complete files. What I can get (bookmarks.json) and what I would like to get (bookmarks_desired.json).

Best answer by debbiatsafe

albinepro wrote:

I have tried this, but still I'm getting the parsing error:

JSONTemplater (XMLTemplaterFactory): The following error occurred near line 5, column 37 of the query:
JSONTemplater (XMLTemplaterFactory): invalid expression: syntax error, unexpected ":", expecting "," or "}"
JSONTemplater (XMLTemplaterFactory): An error occurred while parsing the 'OBLAST' sub-template
JSONTemplater (XMLTemplaterFactory): A fatal error has occurred. Check the logfile above for details
JSONTemplater (XMLTemplaterFactory): A fatal error has occurred. Check the logfile above for details
A fatal error has occurred. Check the logfile above for details

 I have added two workspaces with test data to original posted question with working and not working (desired) workspace.

Hello @albinepro​ 

You have an extra XQuery expression in your OBLAST sub-template. This creates invalid JSON and causes the JSONTemplater to error.

Remove the extra fme:get-attribute("pisemeno") from the OBLAST sub-template so becomes this:

{	
    "oblasti" : fme:get-attribute("pismeno"),
    "caption" : fme:get-attribute("CISLO"),
    "features":[fme:process-features("PRVKY""pismeno", fme:get-attribute("pismeno"))]
}

In addition, remove the pipe operators in the ROOT template as it is unnecessary. Attached is an updated version of your workspace with the suggested changes.

View original
Did this help you find an answer to your question?

4 replies

debbiatsafe
Safer
Forum|alt.badge.img+20

Hi @albinepro​ 

JSON key-value pairs need to be separated by commas. Based on the sub-template you have shared, it appears you are missing these. For example, based on your desired output, your OBLAST sub-template should look something like this:

{
	"oblasti" : fme:get-attribute("pismeno"),
	"caption" : fme:get-attribute("caption"),
	"features": [
		fme:process-features("PRVKY""pismeno", fme:get-attribute("pismeno"))
	]
}

If you're still running into issues after making this change, I would recommend sharing your workspace along with a small sample of your input data. 


albinepro
Supporter
Forum|alt.badge.img+7
  • Author
  • Supporter
  • June 3, 2022
debbiatsafe wrote:

Hi @albinepro​ 

JSON key-value pairs need to be separated by commas. Based on the sub-template you have shared, it appears you are missing these. For example, based on your desired output, your OBLAST sub-template should look something like this:

{
	"oblasti" : fme:get-attribute("pismeno"),
	"caption" : fme:get-attribute("caption"),
	"features": [
		fme:process-features("PRVKY""pismeno", fme:get-attribute("pismeno"))
	]
}

If you're still running into issues after making this change, I would recommend sharing your workspace along with a small sample of your input data. 

I have tried this, but still I'm getting the parsing error:

JSONTemplater (XMLTemplaterFactory): The following error occurred near line 5, column 37 of the query:
JSONTemplater (XMLTemplaterFactory): invalid expression: syntax error, unexpected ":", expecting "," or "}"
JSONTemplater (XMLTemplaterFactory): An error occurred while parsing the 'OBLAST' sub-template
JSONTemplater (XMLTemplaterFactory): A fatal error has occurred. Check the logfile above for details
JSONTemplater (XMLTemplaterFactory): A fatal error has occurred. Check the logfile above for details
A fatal error has occurred. Check the logfile above for details

 I have added two workspaces with test data to original posted question with working and not working (desired) workspace.


debbiatsafe
Safer
Forum|alt.badge.img+20
  • Safer
  • Best Answer
  • June 3, 2022
albinepro wrote:

I have tried this, but still I'm getting the parsing error:

JSONTemplater (XMLTemplaterFactory): The following error occurred near line 5, column 37 of the query:
JSONTemplater (XMLTemplaterFactory): invalid expression: syntax error, unexpected ":", expecting "," or "}"
JSONTemplater (XMLTemplaterFactory): An error occurred while parsing the 'OBLAST' sub-template
JSONTemplater (XMLTemplaterFactory): A fatal error has occurred. Check the logfile above for details
JSONTemplater (XMLTemplaterFactory): A fatal error has occurred. Check the logfile above for details
A fatal error has occurred. Check the logfile above for details

 I have added two workspaces with test data to original posted question with working and not working (desired) workspace.

Hello @albinepro​ 

You have an extra XQuery expression in your OBLAST sub-template. This creates invalid JSON and causes the JSONTemplater to error.

Remove the extra fme:get-attribute("pisemeno") from the OBLAST sub-template so becomes this:

{	
    "oblasti" : fme:get-attribute("pismeno"),
    "caption" : fme:get-attribute("CISLO"),
    "features":[fme:process-features("PRVKY""pismeno", fme:get-attribute("pismeno"))]
}

In addition, remove the pipe operators in the ROOT template as it is unnecessary. Attached is an updated version of your workspace with the suggested changes.


albinepro
Supporter
Forum|alt.badge.img+7
  • Author
  • Supporter
  • June 6, 2022
albinepro wrote:

I have tried this, but still I'm getting the parsing error:

JSONTemplater (XMLTemplaterFactory): The following error occurred near line 5, column 37 of the query:
JSONTemplater (XMLTemplaterFactory): invalid expression: syntax error, unexpected ":", expecting "," or "}"
JSONTemplater (XMLTemplaterFactory): An error occurred while parsing the 'OBLAST' sub-template
JSONTemplater (XMLTemplaterFactory): A fatal error has occurred. Check the logfile above for details
JSONTemplater (XMLTemplaterFactory): A fatal error has occurred. Check the logfile above for details
A fatal error has occurred. Check the logfile above for details

 I have added two workspaces with test data to original posted question with working and not working (desired) workspace.

Great, thanks a lot. Somehow I thought it is essential. The pipe symbols in root hence are not necessarry neither.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings