2016-11-26 18 views
0

バッチスクリプトを作成して、example1.jsonとexample2.jsonという名前の2つのjonsonsの "tags"キーの値を変更しようとしています。jsonのタグフィールドを新しい値に置換する必要があります

入力example1.json

{ 
    "info": { 
    "title": "My example swagger 1", 
    "version": "1.0" 
    }, 
    "paths": { 
    "/v1/resource1": { 
     "get": { 
      "description": "This is the example1", 
      "tags": [ 
       "example1" 
      ], 
      "consumes": [ 
       "*/*" 
      ] 
     } 
    }, 
    "/v1/resource2": { 
     "get": { 
      "tags": [ 
       "example1" 
      ], 
      "consumes": [ 
       "*/*" 
      ] 
     } 
    } 
    } 
} 

入力

{ 
"info": { 
    "title": "My example swagger 2", 
    "version": "1.0" 
    }, 
    "paths": { 
    "/v1/resource3": { 
     "get": { 
      "description": "This is the example2", 
      "tags": [ 
       "example2" 
      ], 
      "consumes": [ 
       "*/*" 
      ] 
     } 
    }, 
    "/v1/resource4": { 
     "get": { 
      "tags": [ 
       "example2" 
      ], 
      "consumes": [ 
       "*/*" 
      ] 
     } 
    } 
    } 
} 

example2.json予想される出力は、同じ名前を持つ2つのjsonsですが、タグの値が変更されました。

Example1.json:

入力タグ値 - (JSONファイル名である)例1

出力タグ値 - (要件ごとなど)tags1

Example2.json

入力タグ値 - example2(jsonファイル名)

出力タグvalue-tags2(要件ごと)

出力Example1.json

{ 
    "info": { 
    "title": "My example swagger 1", 
    "version": "1.0" 
    }, 
    "paths": { 
    "/v1/resource1": { 
     "get": { 
      "description": "This is the example1", 
      "tags": [ 
       "tag1" 
      ], 
      "consumes": [ 
       "*/*" 
      ] 
     } 
    }, 
    "/v1/resource2": { 
     "get": { 
      "tags": [ 
       "tag1" 
      ], 
      "consumes": [ 
       "*/*" 
      ] 
     } 
    } 
    } 
} 

出力リレーExample2.json

{ 
    "info": { 
    "title": "My example swagger 2", 
    "version": "1.0" 
    }, 
    "paths": { 
    "/v1/resource3": { 
     "get": { 
      "description": "This is the example2", 
      "tags": [ 
       "tag2" 
      ], 
      "consumes": [ 
       "*/*" 
      ] 
     } 
    }, 
    "/v1/resource4": { 
     "get": { 
      "tags": [ 
       "tag2" 
      ], 
      "consumes": [ 
       "*/*" 
      ] 
     } 
    } 
    } 
} 

私は、バッチスクリプト内のコマンドの下に書きましたが、SEDが動作しないとして、それは

sed -i "s/$/ #removenewlines#/" %1 
sed -i ":a;N;$!ba;s/\n//g" %1 
sed -i "s/\"tags\":\(.*\) \"%oldtagvalue%\"/\"tags\":\1 \"%newtagvalue%\"/g" %1 
sed -i "s/ #removenewlines#/\n/g" %1 

が動作していません複数の行については、改行を#removenewlines#に変更し、すべてを1行(1行目と2行目)に移動しました。 3行目で、ワイルドカード\(.*\)と変数%oldtagvalue%を使用してキータグを見つけようとしていて、それを%newtagvalue%に置き換えようとしています。 4行目で、私はjsonを書式設定しています。変数の第1のループ値が第2のループ値について

$ echo %1 
$ example1.json 
$ echo %oldtagvalue% 
$ example1 
$ echo %newtagvalue% 
$ tag1 

あろうために 上記のコードは、ループ内で実行

$ echo %1 
$ example2.json 
$ echo %oldtagvalue% 
$ example2 
$ echo %newtagvalue% 
$ tag2 

しかし、それは動作していないであろう。ご意見をお聞かせください。私もjqコマンドを使ってそれを達成しようとしましたが、うまく機能しませんでした。

+0

"JSON"は無効です。そのテキストの塊の実際の意図された構造を理解することは不可能です。それを修正してください。 –

+0

@ApG - 質問は非常に不明です。各タグの値(ファイル内)をどのように変更するかを指定し、期待される出力の例を挙げてください。 – peak

+0

@JeffMercado ..私はjsonを修正しました。ご意見をお聞かせください – ApG

答えて

0

これは、あなたが始める必要があります。

$ jq --arg tag "tag1" ' 
    .paths |= with_entries(.value.get.tags[] = $tag)' Example1.json 

またはわずかに異なる方針で、あなたはこのフィルタを使用できます。どちらの場合も

# Apply f to composite entities recursively, and to atoms 
def walk(f): 
    . as $in 
    | if type == "object" then 
     reduce keys[] as $key 
     ({}; . + { ($key): ($in[$key] | walk(f)) }) | f 
    elif type == "array" then map(walk(f)) | f 
    else f 
    end; 
walk(if type=="object" and has("tags") then .tags[] |= $tag else . end) 

を以下に示すように、出力は次のようになります。もちろん、jqプログラム内で "exampleN"から "tagN"にプログラムで変更することもできます(例えばsub/2を使用してください)。重要な点は、jqだけで仕事をすることができることです。

{ 
    "info": { 
    "title": "My example swagger 1", 
    "version": "1.0" 
    }, 
    "paths": { 
    "/v1/resource1": { 
     "get": { 
     "description": "This is the example1", 
     "tags": [ 
      "tag1" 
     ], 
     "consumes": [ 
      "*/*" 
     ] 
     } 
    }, 
    "/v1/resource2": { 
     "get": { 
     "tags": [ 
      "tag1" 
     ], 
     "consumes": [ 
      "*/*" 
     ] 
     } 
    } 
    } 
} 
1

私が正しく理解していればそれで、あなたは、ファイルの名前を持つタグを交換し、あなたが選んだのタグでそれを交換したいです。ここでは一つの方法ですあなたはそれを行うことができます:

$ jq --arg newtag 'tag1' '(input_filename | sub("\\.json$"; "")) as $oldtag 
    | .paths[][].tags |= map(if . == $oldtag then $newtag else . end)' example1.json 

ファイル名と一致するだけのものを任意のタグを交換したいといない場合は、単に別のパラメータを追加:あなたがなりたかった場合

$ jq --arg oldtag 'example1' --arg newtag 'tag1' \ 
    '.paths[][].tags |= map(if . == $oldtag then $newtag else . end)' example1.json 

を複数の異なるタグを置き換えることができる場合は、古いタグを新しいタグにマッピングしてビルドするオブジェクトを作成できます。

$ jq --argjson tagmap '{"example1":"tag1","example2":"tag2"}' \ 
    '.paths[][].tags |= map($tagmap[.] // .)' example1.json 
関連する問題