Merge two javascript objects without overwriting - javascript

I have two javascript object with data like this:
{
"data": [
{
"foo": "2388163605_10150954953808606",
"bar": {
"xyz": "123",
},
"name": {
"name": [
{
"content": 1,
"data": "some text",
"id": "2388163605"
}
]
},
},
{
"not_the_same": "other values",
"foo": "different",
"bar": {
"xyz": "123",
},
"name": {
"name": [
{
"content": 1,
"data": "some text",
"id": "2388163605"
}
]
},
}]
}
Second one:
{
"data": [
{
"foo": "123+09",
"bar": {
"xyz": "1adad0",
},
},
}]
}
as you can see, the properties etc vary a bit, and of course the values as well. Also they don't have the same number of items in them. I'm trying to merge them like this:
$.extend(true,posts, posts_object);
(posts and posts_objects contain the two objects)
I want to merge them into this:
{
"data": [
{
"foo": "2388163605_10150954953808606",
"bar": {
"xyz": "123",
},
"name": {
"name": [
{
"content": 1,
"data": "some text",
"id": "2388163605"
}
]
},
},
{
"not_the_same": "other values",
"foo": "different",
"bar": {
"xyz": "123",
},
"name": {
"name": [
{
"content": 1,
"data": "some text",
"id": "2388163605"
}
]
},
},
{
"foo": "123+09",
"bar": {
"xyz": "1adad0",
},
},
}]
}
However, this results in the data from the second object to overwrite the data of the first object. Is there any way to merge them but instead add the items from the second object to the first object?

If by "add the items", you mean that you want all properties that are in the second object but not in the first to end up being added to the first one, then:
var tmp;
$.extend(true, tmp, posts);
$.extend(true, posts, posts_object);
$.extend(true, posts, tmp);
might work. It all depends on what copying semantics you want.

Related

Look up values in an array using looping forEach Google Apps Script Javascript

I have an object that looks like the following {key: id numbers}
var obj = {
"c4ecb": {id: [3]},
"a4269": {id: [34,36]},
"d76fa": {id: [54,55,60,61]},
"58cb5": {id: [67]}
}
How do I loop each above id in the following array, and return the label?
var response =
{
"success": true,
"data": [
{
"key": "c4ecb",
"name": "fruits",
"options": [
{
"label": "strawberry",
"id": 3
},
{
"label": "apple",
"id": 4
},
{
"label": "pineapple",
"id": 5
},
{
"label": "Other",
"id": 31
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "a4269",
"name": "vegetables",
"options": [
{
"label": "lettuce",
"id": 34
},
{
"label": "cucumber",
"id": 35
},
{
"label": "radish",
"id": 36
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "d76fa",
"name": "pasta",
"options": [
{
"label": "spaghetti",
"id": 54
},
{
"label": "rigatoni",
"id": 55
},
{
"label": "linguine",
"id": 56
},
{
"label": "lasagna",
"id": 60
},
{
"label": "fettuccine",
"id": 61
}
],
}
]
}
Finally, what I want to do is look up the key and return a string of id values.
For example, input c4ecb and output strawberry. Input a4269 and output lettuce, radish. Input d76fa and output "spaghetti, rigatoni, lasagna, fettuccine"
I think to join the multiple labels output into one string I could use something like
array.data.vegetables.map(vegetables => vegetables.value).join(', ')].toString();
So in the end I want to have something like
var fruits = [some code that outputs "strawberry"];
var vegetables = [some code that outputs "lettuce, radish"];
var pasta = [some code that outputs "spaghetti, rigatoni, lasagna, fettuccine"];
What I've tried so far:
The following loop will return the id only if there is one id to be called for: e.g. only in case one where {id: 3} but returns null in cases like {id: 34,36} (because it's looking for '34,36' in id, which doesn't exist - I need to look for each one individually.
response.data.forEach(({key, options}) => {
if (obj[key]) {
options.forEach(({id, label}) => {
if (id == obj[key].id) obj[key].label = label;
});
}
});
console.log(obj)
Filter the response object to focus on the category that matches the id.
Map over the options array and select the items which appear in obj[id].
Finally convert the filtered results to a string.
See filteredLabelsAsString() function below for implementation.
var obj = {
"c4ecb": {"id": [3]},
"a4269": {"id": [34,36]},
"d76fa": {"id": [54,55,60,61]},
"58cb5": {"id": [67]}
}
var response =
[{
"success": true,
"data": [
{
"key": "c4ecb",
"name": "fruits",
"options": [
{
"label": "strawberry",
"id": 3
},
{
"label": "apple",
"id": 4
},
{
"label": "pineapple",
"id": 5
},
{
"label": "Other",
"id": 31
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "a4269",
"name": "vegetables",
"options": [
{
"label": "lettuce",
"id": 34
},
{
"label": "cucumber",
"id": 35
},
{
"label": "radish",
"id": 36
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "d76fa",
"name": "pasta",
"options": [
{
"label": "spaghetti",
"id": 54
},
{
"label": "rigatoni",
"id": 55
},
{
"label": "linguine",
"id": 56
},
{
"label": "lasagna",
"id": 60
},
{
"label": "fettuccine",
"id": 61
}
],
}
]
}];
function filteredLabelsAsString(obj_key, obj, content=response) {
// sanity check: obj must contain obj_key
if (Object.keys(obj).includes(obj_key)) {
return content.filter((item) => {
// filter content using value of obj_key
return item.data[0].key == obj_key;
}).map((item) => {
// item : { success: true, data: [] }
// map over options array
return item.data[0].options.map((opt) => {
// option : {id, label}
// return the label if the id is in the obj object's list
if (obj[item.data[0].key].id.includes(opt.id))
return opt.label;
}).filter((label) => {
// filter out empty items
return label !== undefined;
});
}).join(",");
}
// if obj does not contain obj_key return empty string
return "";
}
console.log("fruits: " + filteredLabelsAsString("c4ecb", obj));
console.log("vegetables: " + filteredLabelsAsString("a4269", obj));
console.log("pasta: " + filteredLabelsAsString("d76fa", obj));

What is the best way to replace text in json?

So I have a bunch of JSON data and it contains a few fields. for example:
[{
"id": "XXX",
"version": 1,
"head": {
"text": "Main title",
"sub": {
"value": "next"
},
"place": "secondary"
},
"body": [{
"id": "XXX1",
"info": "three little birds",
"extended": {
"spl": {
"text": "song",
"type": {
"value": "a"
}
}
}
},
{
"id": "XXX2",
"info": [
"how are you?"
],
"extended": {
"spl": {
"text": "just",
"non-type": {
"value": "abc"
}
}
}
}
]
}]
what I'm trying to do is kind of conversion table (from a different JSON file)
if a field has the value 'a' replace it with 'some other text..' etc.
I have a service for the JSON pipeline, so I guess this is the right place to do the replacement.
so for this example, I have the JSON above and in my conversion table I have the following terms:
next: forward,
song: music,
a: option1,
just: from
etc...
What you are looking for can be achieved with templates. Replace the variable sections with some specific markers that you can find and replace from some external tools such as perl or sed.
For example, you could have a template.json with something like this:
...
"type": {
"value": "##VALUE##"
}
...
Then when you need the actual JSON, you could pass this though an intermediate script that replaces these templates with actual data.
cat template.json | sed -e 's/##VALUE##/my_value/' > target.json
Alternatively, with Perl:
cat template.json | perl -pi -e 's:\#\#VALUE\#\#:my_value:' > target.json
The best way is to parse it, replace the text in the object, and then stringify it.
The next best way is to use a regular expression.
In this example, I catch exceptions if path cannot be indexed, and use ['type'] instead of .type so it will scale to indexing 'non-type' if you wish.
const data = `[{
"id": "XXX",
"version": 1,
"head": {
"text": "Main title",
"sub": {
"value": "next"
},
"place": "secondary"
},
"body": [{
"id": "XXX1",
"info": "three little birds",
"extended": {
"spl": {
"text": "song",
"type": {
"value": "a"
}
}
}
},
{
"id": "XXX2",
"info": [
"how are you?"
],
"extended": {
"spl": {
"text": "just",
"non-type": {
"value": "abc"
}
}
}
}
]
}]
`
const o = JSON.parse(data)
o[0].body.forEach(b => {
try {
if (b.extended.spl['type'].value === 'a') {
b.extended.spl['type'].value = 'CHANGED'
}
} catch (e) {}
})
const newData = JSON.stringify(o, null, 2)
console.log(newData)
A string replace approach will work if you know and can rely on your source conforming, such as the only "value" is inside "type"
const data = `[{
"id": "XXX",
"version": 1,
"head": {
"text": "Main title",
"sub": {
"value": "next"
},
"place": "secondary"
},
"body": [{
"id": "XXX1",
"info": "three little birds",
"extended": {
"spl": {
"text": "song",
"type": {
"value": "a"
}
}
}
},
{
"id": "XXX2",
"info": [
"how are you?"
],
"extended": {
"spl": {
"text": "just",
"non-type": {
"value": "abc"
}
}
}
}
]
}]
`
const newData = data.replace(/"value": "a"/g, '"value": "NEWVALUE"')
console.log(newData)

Flatten/Reduce nested JSON based on paths, collecting fields in path/hierarchy

UPDATE: 11/18/18: I've added more examples and clarification to the end of this post.
I have nested JSON structures, resulting from Elasticsearch aggregations, that look similar to this (simplified for the example):
{
"some_field": "ignore",
"buckets": [
{
"key": "a",
"buckets": [
{ "key": "foo", "name": "FOO" },
{ "key": "bar", "name": "BAR" }
]
},
{
"key": "b",
"buckets": [
{ "key": "boo", "name": "BOO" },
{ "key": "baa", "name": "BAA" }
]
}
]
}
I would like to convert it to
[
{key: "a", name: "FOO"},
{key: "a", name: "FOO"},
{key: "b", name: "BOO"},
{key: "b", name: "BAA"}
]
While this is a simple problem for any specific case, I don't want to "re-invent the wheel" and code for it each time. The level of nesting can vary and the fields I might pick (along the path) can vary.
The above is somewhat simplified example. As these come from Elasticsearch responses, another example might be:
"aggregations": {
"boo": {
"buckets": [
{
"key": "keyA",
"foo": {
"buckets": [
{
"key": "keyA.a",
"bar": {
"hits": {
"hits": [{"_index": "indexA", "_id": "idA", "_source": {"name": "nameA"}}]
}
}
}
]
}
},
{
"key": "keyB",
"foo": {
"buckets": [
{
"key": "keyA.a",
"bar": {
"hits": {
"hits": [{"_index": "indexB", "_id": "idB", "_source": {"name": "nameB"}}]
}
}
}
]
}
}
]
}
}
and my desired result, for flatten/picking fields is
[
{"boo": "keyA", "foo": "keyA.a", "name": "nameA", "id": "idA"},
{"boo": "keyB", "foo": "keyA.a", "name": "name", "id": "idB"}
]
Does anyone know of a way to do with JMESPath, JSONPath, lodash, etc? With something like JSONPath or JMESPath, I can select the "leaf" value easily, but I am trying to collect fields along the path, in the hierachy.
As noted, I could code each case, but I'd like to reuse a library and declare my projection.
I think I could do this with jq, but I need it to run in a browser.
Thanks
Lodash is useful in your case and can be used in a browser.
flapMap and map functions are what you need.
const _ = require('lodash');
var obj = {
"some_field": "ignore",
"buckets": [
{
"key": "a",
"buckets": [
{ "key": "foo", "name": "FOO" },
{ "key": "bar", "name": "BAR" }
]
},
{
"key": "b",
"buckets": [
{ "key": "boo", "name": "BOO" },
{ "key": "baa", "name": "BAA" }
]
}
]
};
var res = _.flatMap(obj.buckets, x => {
return _.map(x.buckets, y => {
y.key = x.key;
return y;
});
});
console.log(res);
// Output:
// [ { key: 'a', name: 'FOO' },
// { key: 'a', name: 'BAR' },
// { key: 'b', name: 'BOO' },
// { key: 'b', name: 'BAA' }]

push the object to array, according to order number in javascript

Below array i have 5 objects, with random order value, need to arrange the array as per the order key in object
i want push each object another array as per order.
var array = [
{
"name": {
"text": "javascript"
},
"order": {
"text": "4"
}
},
{
"name": {
"text": "angualr js"
},
"order": {
"text": "2"
}
},
{
"name": {
"text": "Ios"
},
"order": {
"text": "3"
}
},
{
"name": {
"text": "PHP"
},
"order": {
"text": "5"
}
}, {
"name": {
"text": "C"
},
"order": {
"text": "5"
}
}
]
can any explain how to follow the logic.
Just sort the array with Array.prototype.sort, it accepts a function as parameter to compare two items.
array.sort(function(a, b) {
return parseInt(a.order.text, 10) - parseInt(b.order.text, 10);
});

getJSON displays [object Object] rather than actual values

I have a JSON and I need to get this JSON and put in the html as a ul li list. It gets the value as object and displays [object Object] in html. If I modify the json then it works. so there is probably something wrong in my script where I am not able to loop throught he json file properly. Can some one help please:
MY JSON IS:
[
{
"us":"USA"
},
{
"fr":"FRANCE"
},
{
"es":"Spain"
},
{
"sa":"South Africa"
}
]
AND JS IS
<script>
$.getJSON('jsonfile', function(data) {
var items = [];
$.each(data ,function(key,val) {
items.push('<li id="'+ key +'">' + val +'</li>');
});
$('<ul />' , {
'class':'new-div',
html:items.join('')
}).appendTo('body');
});
</script>
UPDATED JSON:
[
{
"items":
{
"item":
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}
]
}
}
]
The data you're looping over is the array, which has objects. So your key will be 0, 1, etc., and the val will be the object at that position in the array.
Your JSON structure actually makes it a bit of a pain to output, because each of your objects only has one property, but the property name varies from object to object. You can do it, by looping over the object properties even though there's only one of them:
var items = [];
$.each(data ,function(outerKey, outerVal) { // <== Loops through the array
$.each(outerVal, function(key, val) { // <== "Loops" through each object's properties
items.push('<li id="'+ key +'">' + val +'</li>');
});
});
...but I'd change the JSON structure instead. For instance, assuming the keys are unique, your original code would work with this structure:
{
"us":"USA",
"fr":"FRANCE",
"es":"Spain",
"sa":"South Africa"
}

Categories