这是我编写的一个函数,它将改变/缩短json对象上的所有键
function replaceKeyWithNewKey(jsonObj, new_keys, old_keys){
console.log("test")
for(i=0;i<jsonObj.length;i++){
for(el in jsonObj){
//console.log(new_keys[el])
jsonObj[i][new_keys[el]]=jsonObj[i][old_keys[el]] //add new key
delete jsonObj[i][old_keys[el]] // delete old key
}
}
return jsonObj
}
我正在寻找的是使用bash或python或其他方式在终端中执行此操作的方法,但我想在终端中执行此操作。所以我会在file.json上运行脚本,结果是file2.json,file2.json有更短的键名。 我该怎么做?
对于bash,我正在考虑使用sed
,但我认为这不会像我可能会取代我不想替换的价值一样好。
我知道的小蟒蛇可能是要走的路。
这是一个控制台打印输出:
>data[0]
Object {Rec_Open_Date: "2016-07-07", MSISDN: 123, IMEI: 223, Data_Volume_Bytes: "673", Device_Manufacturer: "Samsung Korea"…}
>old_keys=Object.keys(data[0])
["Rec_Open_Date", "MSISDN", "IMEI", "Data_Volume_Bytes", "Device_Manufacturer", "Device_Model", "Product_Description", "Data_Volume_MB"]
>new_keys=["r", "m", "i", "d", "f", "l", "s", "d2"]
["r", "m", "i", "d", "f", "l", "s", "d2"]
>function replaceKeyWithNewKey(jsonObj, new_keys, old_keys){
console.log("test")
for(i=0;i<jsonObj.length;i++){
for(el in jsonObj){
//console.log(new_keys[el])
jsonObj[i][new_keys[el]]=jsonObj[i][old_keys[el]] //add new key
delete jsonObj[i][old_keys[el]] // delete old key
}
}
return jsonObj
}
undefined
> replaceKeyWithNewKey(data, new_keys, old_keys)
VM129:2 test
[Objectd: "673"d2: "0.000641823"f: "Samsung Korea"i: 223l: "Samsung GT-I9505"m: 123r: "2016-07-07"s: "PREPAY PLUS - $1 - #33"__proto__: Object, Object, Object, Object, Object, Object, Object, Object, Object]
这是测试我的函数的示例数据:
var json = '[{"_id":"5078c3a803ff4197dc81fbfb","email":"user1@gmail.com","image":"some_image_url","name":"Name 1"},{"_id":"5078c3a803ff4197dc81fbfc","email":"user2@gmail.com","image":"some_image_url","name":"Name 2"}]';
答案 0 :(得分:1)
假设您知道如何将json加载到python数据结构中。您提供的示例数据将是list
个dict
的python dict
。我认为最直接的方法是构建一个字典,将旧密钥映射到新密钥,然后使用字典理解迭代列表,以使用密钥名称映射中的新密钥名重建In [8]: jobj = [{"_id":"5078c3a803ff4197dc81fbfb","email":"user1@gmail.com","image":"some_image_url","name":"Name 1"},{"_id":"5078c3a803ff4197dc81fbfc","email":"user2@gmail.com","image":"some_image_url","name":"Name 2"}]
In [9]: keymap = {'email':'e', 'image':'img', 'name':'n', '_id':'id'}
In [10]: for i in range(len(jobj)):
...: jobj[i] = {keymap[k]:jobj[i][k] for k in jobj[i]}
...:
In [11]: jobj
Out[11]:
[{'e': 'user1@gmail.com',
'id': '5078c3a803ff4197dc81fbfb',
'img': 'some_image_url',
'n': 'Name 1'},
{'e': 'user2@gmail.com',
'id': '5078c3a803ff4197dc81fbfc',
'img': 'some_image_url',
'n': 'Name 2'}]
你已经建成了。使用您的样本:
OrderedDict
编辑添加
如果您想确保所有按键都按顺序排列,则需要使用collections
中的import json
from collections import OrderedDict
keymap = {'email':'e', 'image':'img', 'name':'n', '_id':'id'}
with open('ordered_example.json') as f:
jobj = json.load(f, object_pairs_hook=OrderedDict)
for i in range(len(jobj)):
jobj[i] = OrderedDict((keymap[k],jobj[i][k]) for k in jobj[i])
print(jobj)
[OrderedDict([('id', '5078c3a803ff4197dc81fbfb'),
('e', 'user1@gmail.com'),
('img', 'some_image_url'),
('n', 'Name 1')]),
OrderedDict([('id', '5078c3a803ff4197dc81fbfc'),
('e', 'user2@gmail.com'),
('img', 'some_image_url'),
('n', 'Name 2')])]
输出:
unused: false
答案 1 :(得分:1)
如果您要从命令行操作JSON
,我建议您安装jq
。
您可以将键映射放在bash 4+中的关联数组中,如下所示:
declare -A map=([foobar]=foo [poohbah]=pooh [zoowicky]=zoo)
循环遍历它以构建jq
脚本来替换密钥。
jq_script=
for old in "${!map[@]}"; do
new="${map[$old]}"
jq_script+="${jq_script:+|}if has(\"$old\") then { \"$new\": .[\"$old\"] } + del(.[\"$old\"]) else . end"
done
你运行的:
jq "$jq_script" <old.json >new.json
在我的示例JSON上:
{
"foobar": 1,
"zoowicky": 3,
"different": 4
}
它没有地图中的所有键,并且确实有一个不在地图中的键,它会产生这样的结果:
{
"zoo": 3,
"foo": 1,
"different": 4
}
答案 2 :(得分:0)
只需使用sed代替键。
#!/bin/bash
function replaceKeyWithNewKey() {
json_file="$1"
old_key="$2"
new_key="$3"
# you'd better to backup first.
sed -i "" "s/${old_key}/${new_key}/g" ${json_file}
}
old_keys=("Rec_Open_Date" "MSISDN" "IMEI" "Data_Volume_Bytes" "Device_Manufacturer" "Device_Model" "Product_Description" "Data_Volume_MB")
new_keys=("r" "m" "i" "d" "f" "l" "s" "d2")
# input json file
json_file="input"
for ((i = 0; i < ${#old_keys[@]}; ++i)) do
replaceKeyWithNewKey ${json_file} "${old_keys[$i]}" "${new_keys[$i]}"
done