bashスクリプトを使用してjsonマップオブジェクトをマネージドcsv行に変換する

bashスクリプトを使用してjsonマップオブジェクトをマネージドcsv行に変換する

properties以下のように、Customer.jsonファイルのキーの下にjsonマッピングがあります。

{
    "customer": {
        "properties": {
            "customerId": {
                "type": "string",
                "index": "not_analyzed"
            },
            "name": {
                "type": "string",
                "index": "not_analyzed"
            }
        }
    }
}

上記のマッピングを使用keyしてdisplayNameコピーし、次のように変換したいと思います。type

field(key: 'customerId',     displayName: 'customerId', type: 'String')
field(key: 'name',           displayName: 'name',       type: 'String')

試してみました。バッシュ+ Python次のように、まずクライアントキーを取得し、プロパティ内でループを繰り返すとします。

$ cat Customer.json | python -c 'import sys; import simplejson as json; \
print "\n".join( [i["properties"] for i in json.loads( sys.stdin.read() )["customer"]] )'

Traceback (most recent call last):
File "<string>", line 2, in <module>
TypeError: string indices must be integers, not str

他のソリューションにも開いています。

ベストアンサー1

これらの構造化データの解析は、以前と同様に専用のパーサーを使用して実行するのが最善です。ただし、この特別なケースでは、次のことができます。

$ grep -B 1 '"type":' Customer.json | tr $'"' $"'" | sed 's/[:,]//g' | 
    awk '{print "field(key: "$1",\tdisplayName: "$1",\t type: "$NF")"}' RS="--" 

返品:

field(key: 'customerId',    displayName: 'customerId',   type: 'string')
field(key: 'name',  displayName: 'name',     type: 'string')

おすすめ記事