Issue parsing XLIFF with node XML parser - node.js

Im trying to parse XLIFF file using xml2js library. All is working fine but if I have something like that: <source>Welcome to <x id="INTERPOLATION" equiv-text="{{ title }}"/> my friend</source> I will get [{"_":"Welcome to my friend","x":[{"$":{"id":"INTERPOLATION","equiv-text":"{{ title }}"}}]}]. I am basically loosing order for the parts of the sentence. I would expect to get an array of 3 parts:
"Welcome to "
[{"$":{"id":"INTERPOLATION","equiv-text":"{{ title }}"}}]
" my friend"
But instead Im getting:
"Welcome to my friend"
[{"$":{"id":"INTERPOLATION","equiv-text":"{{ title }}"}}]
If I would try to recreate string again I would get <source>Welcome to my friend<x id="INTERPOLATION" equiv-text="{{ title }}"/></source>
Any idea how to solve it with this XML parser or any other?

you also might like txml. When using it like txml.parse(yourXMLString), you get an object like this:
[
{
"tagName": "source",
"attributes": {},
"children": [
"Welcome to ",
{
"tagName": "x",
"attributes": {
"id": "INTERPOLATION",
"equiv-text": "{{ title }}"
},
"children": []
},
" my friend"
]
}
]
I think it looks absolutely as what you are looking for. The three children inside the source, are very clean to use. Also, this parser is only 4kb in size and there is no need for native c compiling that will cause difficulties when running your app on a different architecture.
Disclaimer: I am the author of txml, and this opinion might not be objective ;-)

With fast-xml-parser.
Please, use stopNodes in options when you parse the source.
It makes fast-xml to treat the content as plain-strings
var parser = require("fast-xml-parser");
parser.parse(srcFile, {ignoreAttributes: false, stopNodes: ["source", "target"],});

Related

How to build JSON from remote Swagger documentation (websocket based API)

I am currently grabbing the entire swagger.json contents:
swagger_link = "https://<SWAGGER_URL>/swagger/v1/swagger.json"
swagger_link_contents = requests.get(swagger_link)
#write the returned swagger to a file
swagger_return = swagger_link_contents.json()
with open ("swagger_raw.json", "w") as f:
f.write(json.dumps(swagger_return, indent=4))
The file is filled with beautiful JSON now. But there are a ton of objects in there, quite a few I don't need. Say I wanted to grab the command "UpdateCookieCommand" from my document:
"parameters": [
{
"name": "command",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/UpdateCookieCommand"
},
"x-nullable": false
}
],
but it also has additional mentions later in the document...
UpdateCookieCommand": {
"allOf": [
{
"$ref": "#/definitions/BaseCommand"
},
{
"type": "object",
"required": [
"command",
"id"
],
The latter object is really what I want to take from the document. It tells me what's required for a nominal API command to Update a Cookie. The hope is I could have a script that will look at every release and be able to absorb changes of the swagger and build new commands.
What methodology would you all recommend to accomplish this? I considered making subprocess Linux commands to just awk, sed, grep it to what I want but maybe there's a more elegant way? Also I won't know the amount of lines or location of anything as a new Swagger will be produced each release and my script will need to automatically run with a CI/CD pipeline.

Basic JSON - issue with the bloody bracket

For a while now I've been trying to add Airtouch to HOmebridge and failing miserably.
Current attempt
The original code is this
"platforms": [
{
"platform": "Airtouch",
"name": "Airtouch",
"ip_address": "192.168.0.10",
"ac_include_temps": false,
"units": [
{
"manufacturer": "LG",
"model": "B36AWY-7G6",
"fan": ["AUTO", "QUIET", "LOW", "MEDIUM"]
}
]
}
]
What am I missing? Happy to find someone to be able to troubleshoot & fix :)
Validation error:
Validation error
Thank you all, #user1239299 for sticking around, and #alexanderdavide for sharing the validator tool
I believe I figured out what the error was
I would love to know how to make it perfectly line up though:
enter image description here
At the moment it looks ugly. And when I move the lines of code to match the first picture, and upon saving the config, I get this. The last two brackets caused the issue :) :
enter image description here
The square brackets do align with each other

How can I write or push json to a json file?

So I am building this react app, where I have to create a json structure and then add this to a json file.
Here is the structure I have to build:
{
"name": {
"label": "Name",
"type": "text",
"operators": ["equal", "not_equal"],
"defaultOperator": "not_equal"
},
"age": {
"label": "Age",
"type": "number",
"operators": [
"equal",
"not_equal",
"less",
"less_or_equal",
"greater",
"greater_or_equal",
"between",
"not_between",
"is_empty",
"is_not_empty"
]
},
"gender": {
"label": "Gender",
"type": "select",
"listValues": {
"male": "Male",
"female": "Female"
}
}
}
After finishing with the json structure, I want to push this to a json file, which is the configuration file for a react library (react-awesome-query-builder). Now how can I write to a json file using JS?
I know that I can use Node.js and use fs for this, but I am not sure how to use this in react. Perhaps there is a library I can use to do this?
Can someone point me to the right direction?
You can write it exactly as a traditional text file (because it is basically text). Meanwhile don't forget to give to your file the extension .json!
It is when you will open it afterward that you will have to parse it as json wich is well handled by plenty of libraries
Well... I guess I figured it out. Instead of using an external json file I tested the config file inside as a variable and it works. No need to use read/write from node.js or any of that.

JSONata - JSON to JSON Transformation in Nodejs API

I need to write REST API in Node jS for JSON to JSON transfromation.
There are many library and I sort listed "JSONata"
Please find JSONata simple sample here
The challenge is API receive JSON which has data and map but JSONata require map value without quotes.
{
"data" : {
"title" : "title1",
"description": "description1",
"blog": "This is a blog.",
"date": "11/4/2013"
},
"map" : {
"name": "title",
"info": "description",
"data" : {
"text": "blog",
"date": "date"
}
}
}
but the map object expected by JSONata is like below.
{
"name": title,
"info": description,
"some" : {
"text": blog,
"date": date
}
}
The above JSON key is in Quotes and value without Quotes.
Please find the NodeJS API code.
app.post('/JSONTransform', function(req, res, next)
{
const data = req.body.data;
const map = req.body.map;
var expression = jsonata(map);
var result = expression.evaluate(data);
res.send(result);
});
I can write simple function to remove quotes but the above map is simple example. It can be any no of child object and may have some special character in the value including quotes.
I prefer some npm library or standard way to remove quotes or configure JSONata to accepts quotes in value.
Appreciate if you suggest any other library or option.
This Node JS API is called from ASP.NET Core Web API.
ASP.NET Core Web API gets the data and map from database and pass this as single JSON to Node JS API.
Please suggestion solution to this problem or best alternative.
Thanks
Raj
I found solution to this problem.
Pass the single JSON that has both data and map. Since MAP is not valid JSON, I made entire map as string and escaped doubles quotes which is inside the string.
Please find the sample.
{
"map": "{ \"name\": title, \"info\": description, \"data\": { \"text\": blog, \"date\": date }}",
"data": {
"title": "title1",
"description": "description1",
"blog": "This is a blog.",
"date": "11/4/2013"
}
}

display computed text in JSON format

In an xpage I have several calls to collect data in json format from several notesviews via java class files.
To check or visualize the data I have a "debug mode" option to display this data in computed fields.
The data is json but I would like to have it formatted in the computed text so it is easier to read.
Does anyone know how I can format the display to it is easier to read in stead of one line of text?
e.g. from
{"locationName":"","gender":"Male","companyName":"","name":"Patrick Kwinten","docUNID":"845AB7AF45FF1260C1257E88003DACFA","notesName":"CN=Patrick Kwinten\/O=quintessens","branchName":"Quintessens Global Services","phone": ["+49 1525 161 223"],"info": ["IT Specialsit"],"sourceUNID":"","pictureURL":"http:\/\/dev1\/apps\/banking\/ServiceData.nsf\/0\/845AB7AF45FF1260C1257E88003DACFA\/$FILE\/PortalPicture.jpg","mail": ["patrickkwinten#ghotmail.com"],"reportsTo":"CN=Eva Fahlgren\/O=quintessens","job":"Managaer","departmentName":"Collaboration Services"}
to
{
"locationName": "",
"gender": "Male",
"companyName": "",
"name": "Patrick Kwinten",
"docUNID": "845AB7AF45FF1260C1257E88003DACFA",
"notesName": "CN=Patrick Kwinten\/O=quintessens",
"branchName": "Quintessens Global Services",
"phone": [
"+49 1525 161 223"
],
"info": [
"IT Specialsit"
],
"sourceUNID": "",
"pictureURL": "http:\/\/dev1\/apps\/banking\/ServiceData.nsf\/0\/845AB7AF45FF1260C1257E88003DACFA\/$FILE\/PortalPicture.jpg",
"mail": [
"patrickkwinten#ghotmail.com"
],
"reportsTo": "CN=Eva Fahlgren\/O=quintessens",
"job": "Managaer",
"departmentName": "Collaboration Services"
}
I do it in a different way. I use Google Postman to fire the request (with headers or whatever you need) and then I get the result back in Postman and can view it as "pretty" - this way I don't have to build anything like this into the application - and I also prefer to see the "raw" data and not risk changing anything on manipulating it prior to displaying it the way you suggest :-)
Really can't live without this utility once I discovered it.
/John

Resources