-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathjson2property.js
49 lines (43 loc) · 1.39 KB
/
json2property.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var mappings = [];
var isNumeric = function (n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
var formatJsonPropMappings = function(jsonparent, k, v, context) {
var jsonpath = formatJsonPath(jsonparent,k,v, context);
var mapping = {"json":jsonpath,"value":v};
const vtype = typeof v;
if(vtype === "string" || vtype === 'number') {
const path = (context.enclose) ?
`${context.enclose}${jsonpath}${context.enclose}=${context.enclose}${v}${context.enclose}`
: `${jsonpath}=${v}`;
mappings.push(path);
}
return mapping;
};
var formatJsonPath = function (parent, k, v, context) {
var path = '';
const separator = ((context && context.separator) ? context.separator : '.') || '.';
if (parent) {
if (isNumeric(k)) {
path = parent + '[' + k + ']';
} else {
path = parent + separator + k;
}
} else {
path = k;
}
return path;
};
var traverseBoth = function(jsonObj,parentJson, callback, context) {
if( typeof jsonObj == "object" ) {
$.each(jsonObj, function(k,v) {
// k is either an array index or object key
var mapping = callback(parentJson, k,v, context);
// console.log(JSON.stringify(mapping));
traverseBoth(v,mapping.json,callback, context);
});
}
else {
// jsonOb is a number or string
}
};