We have a json with object like following
JSON Before
// comment 1
{
"version": "2.0.0",
"tasks": [{
"type": "task1",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"aaa": "never"
}
}]
}
// comment 2
JSON After
Now I want to add a new object a new task (task 2)
// comment 1
{
"version": "2.0.0",
"tasks": [{
"type": "task1",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"aaa": "never"
}
},
{
"type": "task2",
"script": "watch",
"problemMatcher": "$tsh",
"isBackground": true,
"presentation": {
"aaa": "never"
}
}
]
}
// comment 2
The trick here, that I need to update the object without changing the structure, lines or comment. I try with jsonParse and it doesnt works
Is it possible in javascript/nodejs ?
I would suggest checking out the comment-json package, this is what it is designed to do, you shouldn't need to roll your own for this.
You can parse the JSON, then add the new task and write to your new file:
const { parse, stringify} = require("comment-json");
const fs = require("fs");
const taskFile = parse(fs.readFileSync("./input.json", "utf8"));
let taskToAdd = {
"type": "task2",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"aaa": "never"
}
};
taskFile.tasks.push(taskToAdd);
fs.writeFileSync("./output.json", stringify(taskFile, null, 4));
input.json
// comment 1
{
"version": "2.0.0",
"tasks": [{
"type": "task1",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"aaa": "never"
}
}]
}
// comment 2
Of course, if you wish to modify the JSON file in place, simply set the input and output filenames to the same value.
Related
How can we convert non-standard hast (which is parsed from a Markdown file) to HTML?
For example, this is the HTML tree data I have:
{
"_path": "/hello",
"_draft": false,
"_partial": false,
"_empty": false,
"title": "Hello!",
"description": "",
"body": {
"type": "root",
"children": [
{
"type": "element",
"tag": "h1",
"props": {
"id": "hello!"
},
"children": [
{
"type": "text",
"value": "Hello!"
}
]
}
],
"toc": {
"title": "",
"searchDepth": 2,
"depth": 2,
"links": []
}
},
"_type": "markdown",
"_id": "content:hello.md",
"_source": "content",
"_file": "hello.md",
"_extension": "md"
}
How can I parse that 'body' bit to HTML in Node.js?
I tried to use hast-util-to-html:
import { toHtml } from 'hast-util-to-html'
console.log('data.body =', toHtml(data.body))
But I got the following error:
TypeError: Cannot read properties of undefined (reading 'toLowerCase')
I think the tag key in the data should be tagName for using hast-util-to-html. tag and prop are non-standard keys I suppose, how can I standardise them?
Strapi v4, postgres.
I have two collections Sprites and Tags and I want to link those collections throught REST API when I creating a new Sprite entry. I have a simple API endpoint which is firing strapi.controller('api::sprite.sprite').create(), but I cant understand how can I specify relation to Tags.
I tried many various variants but nothing not worked for me.
For example:
{
"data": {
"tags": [1, 2],
}
}
As I understand right I need to specify Tag's ID, as said at documentation but its not worked for me.
If I create a new entry from Content Manager and specify relation manually its works good.
Also I tried to open all permissions to Tag collection
Sprites:
{
"kind": "collectionType",
"collectionName": "sprites",
"info": {
"singularName": "sprite",
"pluralName": "sprites",
"displayName": "Sprite",
"description": ""
},
"options": {
"draftAndPublish": true
},
"pluginOptions": {},
"attributes": {
...
"tags": {
"type": "relation",
"relation": "manyToMany",
"target": "api::tag.tag",
"inversedBy": "sprites"
}
}
}
And Tags:
{
"kind": "collectionType",
"collectionName": "tags",
"info": {
"singularName": "tag",
"pluralName": "tags",
"displayName": "tag"
},
"options": {
"draftAndPublish": true
},
"pluginOptions": {},
"attributes": {
"name": {
"type": "string"
},
"sprites": {
"type": "relation",
"relation": "manyToMany",
"target": "api::sprite.sprite",
"inversedBy": "tags"
}
}
}
I am having json objects with parent and child combination. I need to filter only the keys which matches the condition in child key/value pair.
response json is below,
I tried by using lodash filter and pickup and some more thing and result is only getting the child objects. But i need the output with including parent objects as well
"custom": {
"url": "",
"version": ""
},
"apple": {
"path": "www.testing.com",
"version": "4.5"
},
"mango": {
"path": "www.mango.com",
"version": "4.5"
},
"pineapple": {
"path": "www.pineapple.com",
"version": "4.4"
},
"jackfruit": {
"path": "www.jackfruit.com",
"version": "4.3"
}
Here, i need to filter only the objects which has version=4.5 and version="".
i need to filter the above json like below,
"custom": {
"url": "",
"version": ""
},
"apple": {
"path": "www.testing.com",
"version": "4.5"
},
"mango": {
"path": "www.mango.com",
"version": "4.5"
},
Please let me know how to make this possible.
Your question is ambiguity. According to your input/output sample, I guess code below is what you need:
let data = { "custom": {
"url": "",
"version": ""
},
"apple": {
"path": "www.testing.com",
"version": "4.5"
},
"mango": {
"path": "www.mango.com",
"version": "4.5"
},
"pineapple": {
"path": "www.pineapple.com",
"version": "4.4"
},
"jackfruit": {
"path": "www.jackfruit.com",
"version": "4.3"
}}
let result = _.pickBy(data,(v,k)=>v.version === "4.5" || v.version ==="" )
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Gulp watch task runner error. It results in a windows script host error.
I've already tried to change the file task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"command": "gulp",
"isShellCommand": true,
"args": [
"--no-color"
],
"tasks": [
{
"taskName": "scripts",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "sass",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "watch",
"isBuildCommand": true,
"showOutput": "always"
}
]
}
It looks like you are using older syntax. I don't believe taskname is supported anymore for example.
Here is a tasks.json file that I use and both methods of calling gulp commands work. (You mentioned task.json, I assume that is a typo, it is tasks.json)
{
"version": "2.0.0",
"tasks": [
{
"label": "Start server and process files",
"command": "gulp",
"args": [
"sync"
],
"type": "shell",
"options": {
"cwd": "${workspaceRoot}"
}
},
{
"label": "Gulp: Start server only",
"type": "gulp",
"task": "serve",
"problemMatcher": []
},
{
"label": "Gulp: watch",
"type": "gulp",
"task": "watch",
"problemMatcher": []
}
}
It looks like you also want:
"group": "build",
in each of your tasks - that replaces the isBuildCommand syntax you used above.
And:
"presentation": {
"reveal": "always",
},
instead of the showoutput syntax.
See migrating to tasks 2.0 documentation.
I am a newbie to NodeJS and Sails.js.
I want create a REST API that allows me to expand a resource based on query parameter. For eg
HTTP GET /snippets
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://localhost:8000/snippets/1/",
"highlight": "htep://localhost:8000/snippets/1/highlight/",
"title": "test",
"code": "def test():\r\n pass",
"linenos": false,
"language": "Clipper",
"style": "autumn",
"owner": "http://localhost:8000/users/2/",
"extra": "http://localhost:8000/snippetextras/1/"
}
]}
HTTP GET /snippets?expand=owner
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://localhost:8000/snippets/1/",
"highlight": "http://localhost:8000/snippets/1/highlight/",
"title": "test",
"code": "def test():\r\n pass",
"linenos": false,
"language": "Clipper",
"style": "autumn",
"owner": {
"url": "http://localhost:8000/users/2/",
"username": "test",
"email": "test#test.com"
},
"extra": "http://localhost:8000/snippetextras/1/"
}
]}
Wondering how can I do that in Sails.js or NodeJS?
You should use assocations.
Here is how you would create a one-to-many association between your User model and your Snippet model:
// User.js
module.exports = {
// ...
attributes: {
// ...
snippets: {
collection: 'Snippet',
via: 'owner'
}
}
};
// Snippet.js
module.exports = {
// ...
attributes: {
// ...
owner: {
model: 'User'
}
}
};
Then you can hit /snippets if you want a list of snippets, and hit /snippets?populate=[owner] if you need details about the owners of the snippets.