从一个JSON文件中查找值,然后在另一个文件中替换 [英] Lookup values from one JSON file and replace in another

查看:108
本文介绍了从一个JSON文件中查找值,然后在另一个文件中替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件 1. Translation.json

I have 2 files 1. Translation.json

        {
       "sKEY": "CustomField.Account.Preferred_Name_Local_Language.Fieldlabel",
       "label": "Preferred Name",
       "translation": "Nombre Preferido",
        }

2. Form.json

2. Form.json

        {
      "fullName": "Student_Information/Preferred_Name__pc",
      "description": "Preferred Name",
      "inlineHelpText": "Preferred Name",
      "label": "Preferred Name"          
        }

我需要在translation.json中按值查找标签",并将Forms.json中的标签"值替换为translation.json中的翻译"值.

I need to lookup the "label" by value in translation.json and replace the "label" value in Form.json with the "tranlsation" value from translation.json.

推荐答案

上述问题有点令人困惑,但是这里是一个解决方案,假设jq的调用方式如下:

The problem as stated is a bit perplexing, but here is a solution, assuming that jq is invoked like so:

jq -f program.jq —-argfile dict translation.jq form.json

其中program.jq包含:

where program.jq contains:

.label |= if $dict.label == . then $dict.translation else . end

等效地:

if .label == $dict.label then .label = $dict.translation else . end

如果...那么...结束

jq的主"版本允许if ... then ... end,因此上述解决方案可以分别缩短为:

if ... then ... end

The "master" version of jq allows if ... then ... end so that the above solutions can be respectively shortened to:

.label |= if $dict.label == . then $dict.translation end

和:

if .label == $dict.label then .label = $dict.translation end

这篇关于从一个JSON文件中查找值,然后在另一个文件中替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆