Json Joining as in SQL

i have json objects like

{"COLORS":[[1,red],[2,yellow],[3,orange]]}

{"FRUITS":[[1,apple,1],[2,banana,2],[3,orange,3], [4,grenade,1], [5,apple,2]]}

i need to make them like:

{"FRUITS":[[1,apple,red],[2,banana,yellow],[3,orange,orange], [4,grenade,red], [5,apple,yellow]]}

Upvotes: 2

Views: 73

Answers (4)

peak
peak

Reputation: 116750

Here is a simple solution using jq. It assumes that $colors and $fruits contain the JSON values for 'colors' and 'fruits' respectively:

$colors.COLORS | map( {(.[0]|tostring): .[1] } ) | add as $dict
| $fruits
| .FRUITS |= map( (.[2]) |= $dict[tostring] )

The example below shows how to set $coloars and $fruits.

If your jq has INDEX/1, the first line above could be shortened to:

$colors.COLORS | INDEX(.[0]) | map_values(.[1]) as $dict

Explanation

The first line produces a dictionary ($dict), which is then used to make the translation.

Example

Assuming colors.json and fruits.json hold valid JSON values corresponding to the example give in the question, and that program.jq holds the above jq program, the invocation:

jq -n --argfile colors colors.json --argfile fruits fruits.json -f program.jq

yields:

{"FRUITS":[[1,"apple","red"],[2,"banana","yellow"],[3,"orange","orange"],[4,"grenade","red"],[5,"apple","yellow"]]}

Upvotes: 0

jmartins
jmartins

Reputation: 991

You could even use jQuery.each() to help you iterating over array. And also use Array.prototype.filter() to find a color related to id in fruits array.

var colors = {"COLORS":[[1,"red"],[2,"yellow"],[3,"orange"]]};
var fruits = {"FRUITS":[[1,"apple",1],[2,"banana",2],[3,"orange",3], [4,"grenade",1], [5,"apple",2]]}

var f = fruits.FRUITS;

$.each(f, function(i, fruitItem) {
    var colorItem = colors.COLORS.filter(function(color) {
        return color[0] == fruitItem[2]
    });

    fruitItem[2] = colorItem[0][1]
});

console.log(fruits)

Upvotes: 1

Rhumborl
Rhumborl

Reputation: 16609

Another similar method to finnmich, using filter() instead of a nested loop.

This builds a new array and ignores entries which do not have matching ids, like a sql join:

var c = {"COLORS":[[1,"red"],[2,"yellow"],[3,"orange"]]};

var f = {"FRUITS":[[1,"apple",1],[2,"banana",2],[3,"orange",3], [4,"grenade",1], [5,"apple",2]]};

var result = [];

for(var i = 0; i < f.FRUITS.length; i++)
{
    var entry = f.FRUITS[i];
    var id = entry[2];
    var color = c.COLORS.filter(function(v) {
        return v[0] == id;
    });

    if(color.length > 0) {
        result.push([entry[0], entry[1], color[0][1]]);
    }
}

console.dir(result);

Upvotes: 0

finnmich
finnmich

Reputation: 121

I think a simple nested loop is the simplest way to solve this. As far as i know there is no "json join" feature in javascript.

Try this:

var colors = {"COLORS":[[1,"red"],[2,"yellow"],[3,"orange"]]};
var fruits = {"FRUITS":[[1,"apple",1],[2,"banana",2],[3,"orange",3], [4,"grenade",1], [5,"apple",2]]};
console.log(fruits);
for (var i = 0; i < fruits.FRUITS.length; i++) {
	var temp = fruits.FRUITS[i];
	for (var j = 0; j < colors.COLORS.length; j++) {
		if (colors.COLORS[j][0] === temp[2]) {
			temp[2] = colors.COLORS[j][1];
			break;
		}
	};
	fruits.FRUITS[i] = temp;
};
console.log(fruits);

Upvotes: 2

Related Questions