0

I am creating a java script object dynamically in my JS file

    function myfunction (data){
    var illustration_db_data;
    var myData= "";

    if (somecondition){
    illustration_db_data = {
                            "illustration_db_data": [
                                {
                                    "illTitle": "",
                                    "illFile1": "",
                                    "illFile2": "",
                                    "illFile3": "",
                                    "illDesc": "",
                                    "illOwner": "",
                                    "illBc": "",
                                    "illRights": ""
                                }
                            ]
                        };


    else{
    illustration_db_data= "this is my data";

    }

    myData = illustration_db_data

return myData;

    }

Since it is dynamic , it may be java script object or may not be.

Is there any function in JS or jQuery to check if variable is java script object

    if(isJSON(myfunction (data)))
    {

    //do something

var myData = 'myData =' + JSON.stringify(data) + '';
$.ajax({
    type: "POST",
    url: "/myurl",
    data: myData ,
    dataType: "json",
    success: function(data){alert(data);},
    failure: function(errMsg) {
        alert(errMsg);
    }
});

    }
    else{

    //do something else

    }

=============================

Also checked the JSON in http://jsonlint.com/

and it said it is valid JSON

{
    "illustration_db_data": [
        {
            "illTitle": "",
            "illFile1": "",
            "illFile2": "",
            "illFile3": "",
            "illDesc": "",
            "illOwner": "",
            "illBc": "",
            "illRights": ""
        }
    ]
}

=============================

EDIT: It would be good if solution not contain try/catch to avoid unwanted errors

it might seem very simple question but I am asking this with hope that it might help someone like me in future

Hitesh
  • 4,098
  • 11
  • 44
  • 82
  • 1
    Where's the JSON here ? JSON is a text based exchange format. See http://json.org – Denys Séguret Sep 12 '14 at 08:49
  • http://stackoverflow.com/questions/2313630/ajax-check-if-a-string-is-json may be it helps u –  Sep 12 '14 at 08:50
  • 1
    With that code it will **never** be JSON. JSON is a data format, if you have JSON in JavaScript then it will be expressed as a string. – Quentin Sep 12 '14 at 08:50
  • @dystroy : I am trying to create JSON object in my JS and I checked in http://jsonlint.com/ ... it says that it is valid JSON ...I have created first time the JSON object after reading here n there .... Am i creating it wrong ... ?? – Hitesh Sep 12 '14 at 08:54
  • @Touregsys : thanks but can this be done without try/catch ? – Hitesh Sep 12 '14 at 08:55
  • @hitesh sure; http://api.jquery.com/jquery.parsejson/ –  Sep 12 '14 at 08:59
  • 1
    @hitesh — The syntax for JSON is a subset of the syntax for JavaScript. It is possible to write JS where you can extract a portion of it and have valid JSON, but that doesn't make it JSON when it is in the context of your JavaScript. – Quentin Sep 12 '14 at 09:00
  • 1
    @hitesh — Are you trying to determine if your JavaScript source code could be valid JSON if it was so extracted? From within your program? That's probably possibly, but very non-trivial and I don't see any point in trying to do so. What are you actually trying to achieve? – Quentin Sep 12 '14 at 09:01
  • @Quentin :Thanks again ... ok !!!that means I am wrong here ... well learning phase.... could you correct me there ... i wanted to make a JSON object. – Hitesh Sep 12 '14 at 09:02
  • @hitesh — To what end? – Quentin Sep 12 '14 at 09:03
  • @Quentin --> I am sending a ajax call , sending to php file and since data is huge like 11-12 variables so I thought I can just make JSON and send to ajax JSON data – Hitesh Sep 12 '14 at 09:05
  • Duplicate: http://stackoverflow.com/questions/6323338/jquery-ajax-posting-json-to-webservice – Quentin Sep 12 '14 at 09:06
  • this question http://stackoverflow.com/questions/6323338/jquery-ajax-posting-json-to-webservice tells me what I already have done but it does not tell how to validate or find out if it is a JSON object – Hitesh Sep 12 '14 at 09:09
  • anyway three people have already voted for closing this question for some reason ... may be I wont get the answer here in stackoverflow ... Thanks @Quentin for all information it was really helpful – Hitesh Sep 12 '14 at 09:11
  • @hitesh You'll get an answer if you follow advice and clarify your goals. Don't blame SO for your lack of clarity. – Lightness Races in Orbit Sep 12 '14 at 09:22
  • @hitesh — What makes you think that `JSON.stringify` would *not* output valid JSON? – Quentin Sep 12 '14 at 09:22
  • @Quentin : i didnt say it would not !!! but before using the `JSON.stringify` I need to know if it is JSON or a simple string – Hitesh Sep 12 '14 at 09:31
  • @hitesh — It won't be JSON before you put it through `JSON.stringify`. Are you asking how to tell if a variable contains an *object* or a string? Why care anyway? If you do that then you need to guess if the string you get on the server is a string of JSON or a plain string. If you always send JSON then you can just decode it and worry about if you got a string or an object from it on the server. – Quentin Sep 12 '14 at 09:33
  • thanks for idea ... what was I thinking !!! @Quentin : so I am asking question wrong here .. i should ask how to check if it is a string or an object . !!!! – Hitesh Sep 12 '14 at 09:40

1 Answers1

-1

try something like this

var test = '';
test = {name:"SAM"};
alert(typeof test === 'object');// true if it is json object
alert(typeof test === 'string');// false.

test = 'some text';
alert(typeof test === 'object');// false.
alert(typeof test === 'string');// true if it is string.
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40