• 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?

suityou01

Potty-trained
Joined
Jan 20, 2009
Messages
21,122
Likes received
773
Thanks received
222
Location
Clown World
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?
 
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.
 
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?
 
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: 13:52
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?
 
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.
 
I'm sure you might get a better answer on stackoverflow
 
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.
 
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?
 

Advertisers

Back
Top