1

I need to concatenate two Json strings. The code for the first one is below.

var output = {
    Studios: $('#btnStudios button').map(function() {
        return $(this).val();
    }).get(),
    Platform: $('#btnplatforms button').map(function() {
        return $(this).val();
    }).get(),
    VODEST: $('#btnvodest button').map(function() {
            return $(this).val();
        }).get(),
    SDHD: $('#btnsdhd button').map(function() {
            return $(this).val();
        }).get(),
    Category: $('#btncategory button').map(function() {
            return $(this).val();
        }).get(),
    Genre: $('#btngenre button').map(function() {
            return $(this).val();
        }).get(),
};
$('.list').html(JSON.stringify(output));

And the result is:

{"Studios":["warner","tf1","gaumont","pathe","studiocanal","francetv","m6snd"],"Platform":["orange","itunes","sfr"],"VODEST":["vod","est"],"SDHD":["sd","hd"],"Category":["film","tv","others"],"Genre":["thriller","action","drama","horror"]}

I need to add this string to it:

var per = {Period: {"From":["W6"],"To":["W18"]}}

How would I go about doing this?

9
  • Are you sure that is the result? It is not valid JSON. Commented Apr 30, 2015 at 10:00
  • How do you want the result to look like? Commented Apr 30, 2015 at 10:01
  • "I need to add this string to it:" - thats not a string, its another javascript object! Commented Apr 30, 2015 at 10:01
  • The first string yes. The second string i am not sure about(The one wich i want to add to the first one). You could correct me if there is any error. @Kobi Commented Apr 30, 2015 at 10:04
  • I need to add the new object to the availaible string. Does this sound OK? @Jamiec Commented Apr 30, 2015 at 10:06

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This is a good answer, but the first part assumes you're using jQuery (not tagged in original question).
@Jamiec - You are right. I'm sure $('#btnStudios button').map().get() is jQuery, but I am assuming.
Ha, didnt notice that. I think we can safely assume the OP should have been tagged jQuery
Works perfect. Thank you so much. String,Array,Object very confusing. Need to work harder :)
0

One option would be to create a new object and add a property to store the data you need?

var per = {Period: {"From":["W6"],"To":["W18"]}};
per.output = output;
$('.list').html(JSON.stringify(per));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.