Skip to content

Instantly share code, notes, and snippets.

@Jlaird
Last active March 17, 2020 02:25
Show Gist options
  • Save Jlaird/45ecac1946faa7dfb3a625cf7bf34905 to your computer and use it in GitHub Desktop.
Save Jlaird/45ecac1946faa7dfb3a625cf7bf34905 to your computer and use it in GitHub Desktop.
const cols = {
id: "test",
data:
{
columns:
[
{ name: "Date of Birth", dataValue: "response[*].dob"},
{ name: "Name", dataValue: "response[*].name"},
{ name: "hair", dataValue: "response[*].hair"}
]
}
};
const res = {
response:[
{ dob: "555551", name: "bob", hair: "bro"},
{ dob: "555552", name: "carl", hair: "gree"},
{ dob: "555553", name: "reggie", hair: "yell"},
{ dob: "555554", name: "joe", hair: "bla"},
]
};
cols.data.columns.map(col => {
console.log('col', col);
const arr = col.dataValue.split('[');
array = res[arr[0]]
array.map(person => {
console.log('person', person);
})
})
@StevenBCamp
Copy link

StevenBCamp commented Mar 17, 2020

{
    "id": "test",
    "data": 
    {
        "columns": 
        [
        { "name": stuff, "dataValue": "test"},
        { "name": stuff, "dataValue": "test"},
        { "name": stuff, "dataValue": "test"}    
        ]
    }
}

@StevenBCamp
Copy link

{ "id": "test", "data": { "columns": [ { "name": stuff, "dataValue": "test"}, { "name": stuff, "dataValue": "test"}, { "name": stuff, "dataValue": "test"} ] } }

@StevenBCamp
Copy link

StevenBCamp commented Mar 17, 2020

    "id": "test",
    "data":
    {
    "columns":
        [
            { "name": "Date of Birth", "dataValue": "response[*].dob"},
            { "name": "Name", "dataValue": "response[*].name"},
            { "name": "hair", "dataValue": "response[*].hair"}
        ]
    }
}

{
    "response":[
        { "dob": "555551", "name": "bob", "hair": "bro"}
        { "dob": "555552", "name": "carl",  "hair": "gree"}
        { "dob": "555553", "name": "reggie",  "hair": "yell"}
        { "dob": "555554", "name": "joe",  "hair": "bla"}
    ]
}

@StevenBCamp
Copy link

StevenBCamp commented Mar 17, 2020

{ { "name": "bob", "isDirty": false, "hair": "bro"}, { "name": "carl", "isDirty": false, "hair": "gree"}, { "name": "reggie", "isDirty": false, "hair": "yell"}, { "name": "joe", "isDirty": false, "hair": "bla"}, }

@StevenBCamp
Copy link

`

<script type="text/javascript"> /* This work is licensed under Creative Commons GNU LGPL License.
        License: http://creativecommons.org/licenses/LGPL/2.1/
       Version: 0.9
        Author:  Stefan Goessner/2006
        Web:     http://goessner.net/ 
    */
    
    function jsonT(self, rules) {
       var T = {
          output: false,
          init: function() {
             for (var rule in rules)
                if (rule.substr(0,4) != "self")
                   rules["self."+rule] = rules[rule];
             return this;
          },
          apply: function(expr) {
             var trf = function(s){ return s.replace(/{([A-Za-z0-9_\$\.\[\]\'@\(\)]+)}/g, 
                                      function($0,$1){return T.processArg($1, expr);})},
                 x = expr.replace(/\[[0-9]+\]/g, "[*]"), res;
             if (x in rules) {
                if (typeof(rules[x]) == "string")
                   res = trf(rules[x]);
                else if (typeof(rules[x]) == "function")
                   res = trf(rules[x](eval(expr)).toString());
             }
             else 
                res = T.eval(expr);
             return res;
          },
          processArg: function(arg, parentExpr) {
             var expand = function(a,e){return (e=a.replace(/^\$/,e)).substr(0,4)!="self" ? ("self."+e) : e; },
                 res = "";
             T.output = true;
             if (arg.charAt(0) == "@")
                res = eval(arg.replace(/@([A-za-z0-9_]+)\(([A-Za-z0-9_\$\.\[\]\']+)\)/, 
                                       function($0,$1,$2){return "rules['self."+$1+"']("+expand($2,parentExpr)+")";}));
             else if (arg != "$")
                res = T.apply(expand(arg, parentExpr));
             else
                res = T.eval(parentExpr);
             T.output = false;
             return res;
          },
          eval: function(expr) {
             var v = eval(expr), res = "";
             if (typeof(v) != "undefined") {
                if (v instanceof Array) {
                   for (var i=0; i<v.length; i++)
                      if (typeof(v[i]) != "undefined")
                         res += T.apply(expr+"["+i+"]");
                }
                else if (typeof(v) == "object") {
                   for (var m in v)
                      if (typeof(v[m]) != "undefined")
                         res += T.apply(expr+"."+m);
                }
                else if (T.output)
                   res += v;
             }
             return res;
          }
       };
       return T.init().apply("self");
    }
    </script>
    <script type="text/javascript">
        const cols = {
            id: "test",
            data:
            {
              columns:
                [
                  { name: "Date of Birth", dataValue: "response[*].dob"},
                  { name: "Name", dataValue: "response[*].name"},
                  { name: "hair", dataValue: "response[*].hair"}
                ]
            }
            };
            
            const res = {
              response:[
                { dob: "555551", name: "bob", hair: "bro"},
                { dob: "555552", name: "carl",  hair: "gree"},
                { dob: "555553", name: "reggie",  hair: "yell"},
                { dob: "555554", name: "joe",  hair: "bla"},
              ]
            };
            
            
            
            cols.data.columns.map(col => {
              console.log('col', col);
              const arr = jsonPath(res, col.dataValue.substring(0, col.dataValue.indexOf('[*]')));
              array.map(person => {
                console.log('person', person);
              })
            })
    </script>
</head>
<body>

</body>
`

@StevenBCamp
Copy link

`

<script type="text/javascript"> /* JSONPath 0.8.0 - XPath for JSON * * Copyright (c) 2007 Stefan Goessner (goessner.net) * Licensed under the MIT (MIT-LICENSE.txt) licence. */ function jsonPath(obj, expr, arg) { var P = { resultType: arg && arg.resultType || "VALUE", result: [], normalize: function(expr) { var subx = []; return expr.replace(/[\['](\??\(.*?\))[\]']/g, function($0,$1){return "[#"+(subx.push($1)-1)+"]";}) .replace(/'?\.'?|\['?/g, ";") .replace(/;;;|;;/g, ";..;") .replace(/;$|'?\]|'$/g, "") .replace(/#([0-9]+)/g, function($0,$1){return subx[$1];}); }, asPath: function(path) { var x = path.split(";"), p = "$"; for (var i=1,n=x.length; ivar $ = obj;
if (expr && obj && (P.resultType == "VALUE" || P.resultType == "PATH")) {
P.trace(P.normalize(expr).replace(/^$;/,""), obj, "$");
return P.result.length ? P.result : false;
}
}

    </script>
    <script type="text/javascript">
        const cols = {
            id: "test",
            data:
            {
              columns:
                [
                  { name: "Date of Birth", dataValue: "response[*].dob"},
                  { name: "Name", dataValue: "response[*].name"},
                  { name: "hair", dataValue: "response[*].hair"}
                ]
            }
            };
            
            const res = {
              response:[
                { dob: "555551", name: "bob", hair: "bro"},
                { dob: "555552", name: "carl",  hair: "gree"},
                { dob: "555553", name: "reggie",  hair: "yell"},
                { dob: "555554", name: "joe",  hair: "bla"},
              ]
            };
            
            
            
            cols.data.columns.map(col => {
              console.log('col', col);
              const arr = jsonPath(res, col.dataValue.substring(0, col.dataValue.indexOf('[*]')));
              console.log(arr);
              arr.map(person => {
                console.log('person', person);
              })
            })
    </script>
</head>
<body>

</body>
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment