joelvh / json2json

Transform (reformat) JSON structures from one to another using JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Data-driven Formatting Example

aaronkaka opened this issue · comments

Would you provide an example in the documentation of how to format the output conditionally based on the value of a specific key? I'm attempting to perform data-driven styling, but having difficulty with the format function.

For example:

var tmpl = {
path: 'response.docs',
format: function (node, value, key) {

// pseudo-code to express the desired feature
// if (some.key.value > 50)
// return {color.value = 'red'};
},
as: {
title: 'Title',
effortDriven: 'effortDriven',
color: 'blue'
}
};

The format property can be a method or a hash (associative array) of keys with their own functions. You would want to use the "node" parameter to reference any other properties, but can't really look at any parent nodes.

Maybe something like this would work for you? (I haven't tested this, but it should work.)

var tmpl = {
    path: 'response.docs',
    format: {
        // formatter specifically for the "blue" property in the original document (from below)
        blue: function (node, value, key) {
            // reference "Count" in the original document (from below)
            if (node.Count > 50) {
                return { key: "color", value: "red" }
            }
        }
    },
    as: {
        title: 'Title',
        count: 'Count',
        effortDriven: 'effortDriven',
        color: 'blue'
    }
};

Given the source JSON:

{"responseHeader":{
"status":0,
"QTime":2534,
"params":{
"fl":"PrimaryKey,Category,Title",
"q":"SearchField:Health",
"wt":"json",
"rows":"10"
}
}, "response":{
"numFound":96256,
"start":0,
"maxScore":2.274466,
"docs":[
{
"PrimaryKey":"Wikipedia/page/486187da",
"Title":"User:Alhikmah",
"duration":"5 days",
"percentComplete":26,
"start":"01/01/2009",
"finish":"01/05/2009",
"effortDriven":true
}
]
}}

This is the template:

        var gridRuleSet = {
            path: 'response.docs',
            format: {
                // formatter specifically for the "blue" property in

the original document (from below)
blue: function (node, value, key) {
// reference "Count" in the original document (from
below)
if (parseInt(node.percentComplete) > 25) {
return { key: "color", value: "red" };
} else {
return { key: "color", value: "blue" };
}
}
},
as: {
title: 'Title',
duration: 'duration',
percentComplete: 'percentComplete',
start: 'start',
finish: 'finish',
effortDriven: 'effortDriven',
color: 'blue'
}
};

The color attribute never shows up in the output (regardless of whether the
parseInt is there or not). Am I doing something wrong?

On Mon, Dec 10, 2012 at 5:47 PM, Joel Van Horn notifications@github.comwrote:

The format property can be a method or a hash (associative array) of keys
with their own functions. You would want to use the "node" parameter to
reference any other properties, but can't really look at any parent nodes.

Maybe something like this would work for you? (I haven't tested this, but
it should work.)

var tmpl = {
path: 'response.docs',
format: {
// formatter specifically for the "blue" property in the original document (from below)
blue: function (node, value, key) {
// reference "Count" in the original document (from below)
if (node.Count > 50) {
return { key: "color", value: "red" }
}
}
},
as: {
title: 'Title',
count: 'Count',
effortDriven: 'effortDriven',
color: 'blue'
}
};


Reply to this email directly or view it on GitHubhttps://github.com//issues/4#issuecomment-11226184.

@aaronkaka did you ever figure this out? Is there an update to the README or elsewhere you'd like to submit?

@aaronkaka anything you can contribute here?