Concatenate Json to a String in JavaScript

How to append Json String to another Json String in javascript

You could convert them to JS objects and combine them

So:

const obj1 = JSON.parse(json1);
const obj2 = JSON.parse(json2);
const obj3 = JSON.parse(json3);

const mergedObj = Object.assign(obj1, obj2, obj3);

const jsonStr = JSON.stringify(mergedObj);

jsonStr should have the three JSONs combined


I just saw that you use JSON.stringify() to get your strings, so if you have three objects, just do the Object.assign() portion and you should be good.

Join JSON Object member string values together

This looks like a job for $.map!

var data = {
"category": [{
"id": 28,
"name": "Dogs"
},
{
"id": 14,
"name": "Cats"
},
{
"id": 878,
"name": "Sheep"
}]
}

var cats = $.map(data.category, function(v){
return v.name;
}).join(', ');

concatenate string to json object

var events is not JSON. It is a literal javascript array, you should not be concatenating it, but simply nesting it in a new object and then serializing into JSON if you need a JSON string.

var events = [ 
{
date: "28/10/2013",
title: 'DUE DAY ENROLMENT',
},
{
date: "29/10/2013",
title: 'DUE DAY PAYMENT',
},
{
date: "31/10/2013",
title: '1st DAY OF CLASS',
},
];

var nestedEvents = { 'events': events };
var jsonEvents = JSON.stringify(nestedEvents);

As a general rule of thumb, if you find yourself tempted to manually build a JSON string, you are probably not taking the right approach. Build the data structure you want to serialize into JSON first, then serialize it.

The other takeaway, which seems to be a common point of confusion for developers, is that JSON is nothing more than a serialized string representation of some data structure. There is no such thing as a JSON object. The JSON format certainly bears a strong resemblance to a javascript object literal, but it is truly different and should be treated as such. In this case your events variable does not contain a JSON string, so you should not expect to be able to concatenate it as if it were a string.

Concatenate JSON to a string in Javascript

You're mis-interpreting reduce's work. It doesn't refer to the variables content and res which are declared prior to it. Instead, it loops over the text array, and concatenate the object to the string.

Note that the variables content and res in the callback are local to the callback, and are given preference over the ones which were define before reduce executes.

You can try the following approaches to correct the code. If you simply require the concatenation, do this:

var result = content + ' ' + res;

Or if you want to use reduce for this, do it like this:

var text = ['here is this', {  a: 'b',  c: 'd'}]
var result = text.reduce((content, res) => content += " " + (typeof res === 'object' ? JSON.stringify(res) : res));console.log(result)

JSON data concatenate two data

Use JSON.parse to parse your data into JavaScript object and then with Array#map function iterate over the items and populate your custom objects.

const dataAsJson = `[  {    "lastname": "Stark",    "firstname": "Tony",    "id": 1  },  {    "lastname": "Parker",    "firstname": "Peter",    "id": 2  },  {    "lastname": "Rogers",    "firstname": "Steve",    "id": 3  }]`;
const data = JSON.parse(dataAsJson);
const changedData = data.map(item => ({ fullname: `${item.firstname} ${item.lastname}`, id: item.id }));
console.log(changedData);

Concatenate Json strings

If you are using jQuery, you can use $.extend

$.extend(output, per);

before you call JSON.stringify(output).

This will copy all properties from per to output.

You can also do that manually:

output.Period = per.Period;

There is no reason to work with strings here - you can manipulate the JavaScript object, and convert them to a JSON string when you're done. If you do have a string, you can always parse it to an object using:

var output = JSON.parse(firstString);

But again, you could have done that better.

How to concatenate string with JSON in react.js

If I've understood your question right I think you could do the following:

fetchData(params, id){
axios({
url:`/admin/${this.state.radioSelect}/detail`,
method:'get',
params
}).then(resp => {
if(resp.data.code == '200'){
const updatedData = resp.data.data.map(element => ({
...element,
location: element.location + resp.data.location
}))

this.setState({
pageSize: this.state.pagination.pageSize,
records: updatedData,
recordsTableSpin: false,
recordsId: id
})
}
})
}


Related Topics



Leave a reply



Submit