Processing and setting of translating Json files in SDL Trados

Posted by benson on Fri, 31 Dec 2021 02:07:12 +0100

What is a JSON file

JSON file is a file used to store simple data structures and objects, which can be used for data exchange in web applications; JSON, fully known as JavaScript Object Notation, is an organized and easy to access method for storing information.

JSON files can be converted to XML files

Online JSONXML transformation

Compare JSON and XML

Take the following fragment as an example

JSON

{
	"store": {
		"book": [{
				"category": "reference",
				"author": "Nigel Rees",
				"title": "Sayings of the Century",
				"price": 8.95
			}, {
				"category": "fiction",
				"author": "Evelyn Waugh",
				"title": "Sword of Honour",
				"price": 12.99
			}, {
				"category": "fiction",
				"author": "Herman Melville",
				"title": "Moby Dick",
				"isbn": "0-553-21311-3",
				"price": 8.99
			}, {
				"category": "fiction",
				"author": "J. R. R. Tolkien",
				"title": "The Lord of the Rings",
				"isbn": "0-395-19395-8",
				"price": 22.99
			}
		],
		"bicycle": {
			"color": "red",
			"price": 19.95
		}
	}
}

XML

<?xml version="1.0" encoding="UTF-8" ?>
<store>
	<book>
		<category>reference</category>
		<author>Nigel Rees</author>
		<title>Sayings of the Century</title>
		<price>8.95</price>
	</book>
	<book>
		<category>fiction</category>
		<author>Evelyn Waugh</author>
		<title>Sword of Honour</title>
		<price>12.99</price>
	</book>
	<book>
		<category>fiction</category>
		<author>Herman Melville</author>
		<title>Moby Dick</title>
		<isbn>0-553-21311-3</isbn>
		<price>8.99</price>
	</book>
	<book>
		<category>fiction</category>
		<author>J. R. R. Tolkien</author>
		<title>The Lord of the Rings</title>
		<isbn>0-395-19395-8</isbn>
		<price>22.99</price>
	</book>
	<bicycle>
		<color>red</color>
		<price>19.95</price>
	</bicycle>
</store>

JSON benefits

  • JSON files are smaller than XML, and the more data content, the greater the difference between them

JSON disadvantages

  • JSON quotation marks need to be escaped and written as ", although XML also has five entities, including quotation marks, that need to be escaped, at present, XML tools are relatively perfect, and many of them can be escaped automatically, but JSON is not so popular
  • JSON has no header flag, compared with the <? XML version="1.0" encoding="UTF-8" ?> Tag versions and file encoding formats are sometimes important for different languages and countries
  • XML has two parsing methods: DOM and SAX, and there is only one JSON at present

JSON translation processing

SDL Trados Studio has built-in JSON processing parser

All JSON contents are processed by default, and JSON path customization and embedded content processing are supported

JSON PATH

JSON PATH is a processing language similar to XPATH. It selects JSON files, which is very close to XPATH in syntax
Syntax points:

  • $represents the root element of the document
  • @Represents the current element of the document
  • . node_ Name or ['node_name '] matches child nodes
  • [index] retrieves the elements in the array
  • [start:end:step] supports array slicing syntax
  • *As a wildcard, match all members
  • .. Child recursive wildcard that matches all child elements of a member
  • (< expr >) use expressions
  • ? (< Boolean expr >) for data filtering

Compare XPATH

XPathJsonPathexplain
/$Document root element
.@Current element
/. Or []Match child elements
...N/AThis operator is not supported by JsonPath for matching parent elements
//...Recursively match all child elements
**Wildcards, matching subordinate elements
@N/AMatching property, this operator is not supported by JsonPath
[][]Subscript operator to obtain elements according to the index. XPath index starts from 1 and JsonPath index starts from 0
|[,]Join operator, which splices multiple results into an array and returns them. You can use index or alias
N/A[start:end:step]Data slicing operation, XPath does not support
[]?()Filter expression
N/A()Script expression, using the underlying script engine, XPath does not support
()N/AGrouping, not supported by JsonPath
  • The index of JsonPath starts counting from 0 and the XPATH index starts counting from 1

Comparison of the above example files

XPathJsonPathexplain
/store/book/author$.store.book[*].authorauthor nodes for all book s
//author$...authorAll author nodes
/store/*$.store.*All nodes under store, book array and bicycle nodes
/store//price$.store...priceAll price nodes under store
//book[3]$...book[2]Match the 3rd book node
//book[last()]$... book[(@.length-1)], or $... book[-1:]Match the penultimate book node
//book[position()<3]$... book[0,1], or $... book[:2]Match the first two book nodes
//book[isbn]$...book[?(@.isbn)]Filter nodes with isbn fields
//book[price<10]$...book[?(@.price<10)]Filter nodes with price < 10
//*$...*Recursively match all child nodes

For the above example, there are book and Bicycle in the Store. Only the description related to book is translated. The settings are as follows

Three rules are added for category, author and title

$.store.book[*].category
$.store.book[*].author
$.store.book[*].title

The results are as follows

Comparison and summary

The JSON parser is still very limited in function. For example, it can only set the part to be translated, and cannot be set reversely. If the conditions are met, the content will not be translated. In addition, the implementation

Topics: JSON