从 *.txt文件更新多个 *JSON文件
我有几个JSON文件,每个文件包含以下行,
"description": "Example",
我需要在每个JSON文件中使用文本文件中的一行更新此字段,例如,文本文件具有以下内容,每个文件都包含在单独的行上。
蓝色牛仔裤
红色外套
帽子
衬衫
花卉连衣裙
毛门夹克
绿带
鞋
橙色衬衫
带
我想看看是否有一个方法或脚本,每个json文件都可以通过文本文件并更新描述字段,1.JSON文件将从文本文件等返回行项目1,并完成工作直到完成,10.JSON将从 *.txt文件等订单10中更新
。 “,已更新为“描述”:“ HAT”,
任何帮助都将不胜
感激
I have several json files, each file contains the following line
"description": "Example",
I need to update this field in each json file with a line from the text file, as an example the text file has the following, each contained on a separate line.
Blue Jeans
Red Jacket
Hat
Shirt
Floral Dress
Wollen Jacket
Green belt
Shoes
Orange Shirt
Belt
I'm trying to see if there is a way or script that for each json file to go through the text file and update the description field, 1.json file would return line item 1 from the text file etc and work its way through until completed, 10.json would update with line item 10 from the *.txt file etc
ie file 3.json would have "description": "Example",
updated to "description": "Hat",
Any help would be appreciated
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是读取每个文件,解析它,然后更新您需要更新的内容,然后最终将其再次写回磁盘。
这是我的
test.js
脚本:我已将其测试到文件:
1.JSON
2.json
然后我使用
节点test.js ,此结果在以下结果:
console
1.JSON
2.JSON
编辑
如您提到的 在评论中,您想从此处的文本文件中读取
替换
您需要添加到上一个代码中的内容。replacements.txt
在此处填充
替换的代码
给定的阵列上述文件,每个元素都在新行上,如您在评论中所述:One way to do it is to read each file, parse it, then update what you need to update and then finally write it back to disk again.
Here my
test.js
script:I have tested it with to files:
1.json
2.json
Then I run the script using
node test.js
and this result in the following:Console
1.json
2.json
Edit
As you've mentioned in the comment you want to read the
replacements
from a text file here what you need to add to the previous code.replacements.txt
Here the code to populate the
replacements
array given you have above file with every element being on a new line as you've described in your comment: