编辑JSON - 添加属性

时间:2017-08-04 16:36:23

标签: json attributes add

我有大量的JSON文件,我正在获取转储,其中包含来自当天/期间的数据。我正在处理的大多数JSON文件比这要大很多,但我认为较小的JSON文件会更容易使用。

{"playlists":[{"uri":"spotify:user:11130196075:playlist:1Ov4b3NkyzIMwfY9E8ixpE","listeners":366,"streams":386,"dateAdded":"2016-02-24","newListeners":327,"title":"#Covers","owner":"Saga Prommeedet"},{"uri":"spotify:user:mickeyrose30:playlist:2Ov4b3NkyzIMwfY9E8ixpE","listeners":229,"streams":263,"dateAdded":"removed","newListeners":154,"title":"bestcovers2016","owner":"Mickey Rose"}],"top":2,"total":53820}

我实际上要做的是为每行数据添加一个日期属性,这样当我组合多个JSON文件以通过分析工具时,正确的数据行与正确的日期相关联。我的第一个想法是这样写:

{"playlists":[{"uri":"spotify:user:11130196075:playlist:1Ov4b3NkyzIMwfY9E8ixpE","listeners":366,"streams":386,"dateAdded":"2016-02-24","newListeners":327,"title":"#Covers","owner":"Saga Prommeedet"},{"uri":"spotify:user:mickeyrose30:playlist:2Ov4b3NkyzIMwfY9E8ixpE","listeners":229,"streams":263,"dateAdded":"removed","newListeners":154,"title":"bestcovers2016","owner":"Mickey Rose"}],"top":2,"total":53820,"date":072617}

因为当我通过像Tableau这样的分析工具时,“top”和“total”属性显示在每一行数据上(相关值也显示在每一行上)。

此外,一直在通过Brackets编辑和保存文件,并通过此转换器测试内容(https://konklone.io/json/

1 个答案:

答案 0 :(得分:0)

用javascript语言

var m = JSON.parse(json_string);
m["date"]="20170804";
JSON.stringify(m);

这对您有用,非常简单,