• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Create empty typed JSON object?

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Create empty typed JSON object?

    How can I create an empty typed JSON object in javascript?

    For example

    Code:
    var data={"users":[
            {
                "firstName":"Ray",
                "lastName":"Villalobos",
                "joined":2012
            },
            {
                "firstName":"John",
                "lastName":"Jones",
                "joined":2010
            }
    ]}
    Creates me a JSON object with two items in the array "users".

    What if I wanted users to be strongly typed, but empty? Then add items using the push method?

    Any ideas?
    Knock first as I might be balancing my chakras.

    #2
    Is there such a thing as strongly typed JSON?
    I was under the impression that by definition it is a loosely typed data type.

    To be strongly typed you would need to publish the definition of the JSON object and then attach the JSON object to its definition, much in the same way that XML Schema work.
    Coffee's for closers

    Comment


      #3
      Originally posted by Spacecadet View Post
      Is there such a thing as strongly typed JSON?
      I was under the impression that by definition it is a loosely typed data type.

      To be strongly typed you would need to publish the definition of the JSON object and then attach the JSON object to its definition, much in the same way that XML Schema work.
      Yep, good with that. Care to post an example?
      Knock first as I might be balancing my chakras.

      Comment


        #4
        Originally posted by suityou01 View Post
        How can I create an empty typed JSON object in javascript?

        For example

        Code:
        var data={"users":[
                {
                    "firstName":"Ray",
                    "lastName":"Villalobos",
                    "joined":2012
                },
                {
                    "firstName":"John",
                    "lastName":"Jones",
                    "joined":2010
                }
        ]}
        Creates me a JSON object with two items in the array "users".

        What if I wanted users to be strongly typed, but empty? Then add items using the push method?

        Any ideas?
        Just pass empty values for name, firstname joined. This will give you an empty typed json object.
        Last edited by proggy; 29 May 2013, 12:55.

        Comment


          #5
          Originally posted by proggy View Post
          Just pass empty values for name, firstname joined.
          Not quite what I meant. Perhaps I explained badly.

          I can create a JSON object, and strongly type the properties of a "row". What if I wanted an empty JSON object? Ie NO rows, but strongly typed.

          I know how to create a JSON schema to validation a JSON object, but say for instance, how would I create a JSON object from a schema, but not have any rows in it?
          Knock first as I might be balancing my chakras.

          Comment


            #6
            Originally posted by suityou01 View Post
            Yep, good with that. Care to post an example?
            Sorry - I was talking theoretically. I don't think JSON supports that sort of activity!
            Coffee's for closers

            Comment


              #7
              Originally posted by suityou01 View Post
              Not quite what I meant. Perhaps I explained badly.

              I can create a JSON object, and strongly type the properties of a "row". What if I wanted an empty JSON object? Ie NO rows, but strongly typed.

              I know how to create a JSON schema to validation a JSON object, but say for instance, how would I create a JSON object from a schema, but not have any rows in it?
              I don't think you can mate, you can throw any old stuff into a Json array.

              Comment


                #8
                I'm sure you might get a better answer on stackoverflow

                Comment


                  #9
                  Originally posted by proggy View Post
                  I don't think you can mate, you can throw any old stuff into a Json array.
                  OK, I'm happy with the typing aspect. The typing aspects don't bother me at all. It is purely semantics as far as this problem is concerned.

                  What I am after is an empty JSON object, that defines the dimensions of the "array".

                  If you thought of the JSON object as a humble database table, I can define the columns but not have any rows. That is what I need. A JSON object with only columns.
                  Knock first as I might be balancing my chakras.

                  Comment


                    #10
                    Originally posted by suityou01 View Post
                    OK, I'm happy with the typing aspect. The typing aspects don't bother me at all. It is purely semantics as far as this problem is concerned.

                    What I am after is an empty JSON object, that defines the dimensions of the "array".

                    If you thought of the JSON object as a humble database table, I can define the columns but not have any rows. That is what I need. A JSON object with only columns.
                    You can't, perhaps JSON isn't the most appropriate notation for your problem?

                    Comment

                    Working...
                    X