Convert Table From InDesign to JSON-Format

At my work I have been assigned a challenging task.

I need to convert 125 InDesign product sheets to a JSON-file for our customers data integrator.

They have given me some specs on how they want the data specified, and I would like to automate some of it with Keyboard Maestro – but I am stuck on the tables and hope somebody can help me.

There are 2 types of tables:

Table A should be defined like this in JSON:

 "Optimale virkningsforhold":
 {
	"Temperatur":{
		"Minimum": "6 C",
		"Optimum": "10 - 12 C"
	},
	"Luftfugtighed":{
		"Optimum": "70%"
	}
 },

I get this when I copy from InDesign:

	Minimum	Optimum
Temperatur	6 °C	10 - 12°C
Luftfugtighed		70%
Jordfugtighed		Høj
Timer tørvejr		1

And this is the definition for Table B:

"Afgrøde":{
	"Bederoer, Rødbeder":{
		"Skadegørene":"Flyvehavre...",
		"Dosis pr. ha.": "0,5-0,8 l",
		"Tidspunkt": "Fra afgr'ders stadium 11"
	},
	"Bederoer, Rødbeder":{
		"Skadegørene":"Kvik",
		"Dosis pr. ha.": "0,75-0,8 l",
		"Tidspunkt": "Fra afgr'ders stadium 11"
	}
}

And this is from InDesign:

Afgrøde 	Skadegørere	Dosis pr. ha.	Tidspunkt
Bederoer, Rødbeder	Kvik	0,75-0,8 l	Fra afgrødens stadium 11
Bederoer, Rødbeder	Flyvehavre, græsukrudt, spildkorn	0,5-0,8 l	Fra afgrødens stadium 11
Kartofler	Kvik	0,75-0,8 l	Fra afgrødens stadium 11
Kartofler	Flyvehavre, græsukrudt, spildkorn	0,5-0,8 l	Fra afgrødens stadium 11

Hope this makes sense and somebody has a good solution.

Because InDesign is separating the columns with tab characters, you should be able to find a TSV (tab-separated values) to JSON converter with a bit of Googling. Search also for a CSV (comma-separated values) to JSON converter, as most CSV parsers have options for handling TSV. You may have to tweak the data before doing the conversion—the first column of Table A, for example, doesn't have a header.

However, I am worried about the JSON you're supposed to produce. The Table B example, in particular, is concerning because it has duplicate keys. While I believe this is legal JSON, it is very much frowned upon—some JSON processors will overwrite previous values when duplicate keys are encountered. A more reasonable JSON representation of the top two rows of Table B would be

[
  {
    "Afgrøde": "Bederoer, Rødbeder",
    "Skadegørere": "Kvik",
    "Dosis pr. ha.": "0,75-0,8 l",
    "Tidspunkt": "Fra afgrødens stadium 11"
  },
  {
    "Afgrøde": "Bederoer, Rødbeder",
    "Skadegørere": "Flyvehavre, græsukrudt, spildkorn",
    "Dosis pr. ha.": "0,5-0,8 l",
    "Tidspunkt": "Fra afgrødens stadium 11"
  }
]

Have the people who specified the JSON output thought carefully about what they want and how it's going to be processed? They could end up losing a lot of data.

Finally, I noticed that the InDesign output for Table B is missing the third row. Is that just a copy/paste error, or did it really fail to pick up that row? That sort of failure will lead to more work on your part.

1 Like

Thanks for the input.

I suspect the specs I have received for delivering data are some they have invented themselves.
I also wondered why there is a difference in how they would like to the different types of tables.
I will try to see if they will accept a more common or standard way to define the data.

In regards to the missing row I removed it manually in the example because it included carriage returns in the cells. And I will fix that any other way before the copying from InDesign.

And then I will look into the converters.

FOLLOW-UP:

I got the data integrator to accept the more standard definition of the JSON-data, so I ended up using EasyDataTransforms cli function to convert from tsv to JSON.

2 Likes