1

I have two json objects data and foo, i want to merge them and create a single json object. The resultant json should have all the values from foo and the values from data. so far i have something like this

javascript:

  $('#btn').click(function(){
            var imdbid=$('#tst').val();
            var url = "http://www.omdbapi.com/?i="+imdbid+"&plot=full&r=json"
            $.ajax({
            url:url,
            dataType:'json',
            success:function (json) {
                 data=json
                 var foo=$('form').serializeJSON() // store json string 
                 var marged=$.extend(data,foo);
                 console.log(marged);
                }
            })   
            }) 

HTML:

<form id="myform">
<label>imdb id:</label><input type="text"  id="tst" name="tst"/></form><br/>
<label>comment:-</label><input type="text" id="comment" name="comment" /><br/>
<label>link:-</label><input type="text" name="link" id="link" /><br/>
<input type="button" value="search"  id="btn"/>
</form>

the first object contains data returned from api and foo contains the values of form.i want to marge both object on a success function and send to php. when i am trying to print out the data it's as follows

{Title: "Mother's Day", Year: "2016", Rated: "PG-13", Released: "29 Apr 2016", Runtime: "118 min"…}
Actors
:
"Britt Robertson, Jennifer Aniston, Julia Roberts, Timothy Olyphant"
Awards
:

imdbID
:
"tt4824302"
tst
:
"tt4824302"

it's getting first input value not showing the second two.

4
  • 1
    There's no such thing as a "JSON object" Commented May 1, 2016 at 18:24
  • Note that serializeJSON returns a string, you can't merge a string with an object using $.extend, as it only merges two objects. Commented May 1, 2016 at 18:26
  • Then i have to convert that string to object first ?or is there any other way to accomplish this ? Commented May 1, 2016 at 18:29
  • 1
    $.extend(data, JSON.parse(foo));, or use jQuery's serializeArray Commented May 1, 2016 at 18:32

2 Answers 2

1

You're closing the FORM on this line:

<label>imdb id:</label><input type="text"  id="tst" name="tst"/></form><br/>

... so it only gets the first input field ; )

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

1 Comment

ahh yeah exactly. i knew there was something wrong with the form but didn't notice that.Thank you now it is working
1

first change json to object than merge them as

data=json

var foo=$('form').serializeJSON();

marged=$.extend(JSON.parse(data),JSON.parse(foo));

console.log(marged);

1 Comment

it returns everything form the first object data but only there are input name="comment" and input name="link" are missing when console.log the data

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.