Jump to content
Gilbert

JSON question - simple

Recommended Posts

Hi, I've read the json tutorial on w3 website, and just have one question which makes a huge difference in the way I create the layout of my json file.   All the examples they gave had a very short json file to read, but how does a json file look when you're trying to read many records.  Does each httpRequest read the whole file from one first '{' to the last '}' and that's it.  Or, if there are several, {'s will it return each one as an object.   It was unclear to me from the tutorials.  Specifically, If I had  a database with 10 records and 5 fields in each, would I do this:

{ "name1" : "value1",  "name2" : "value2", "name3" : "value3",  "name4" : "value4", "name5" : "value5" }

{ "name1" : "value1",  "name2" : "value2", "name3" : "value3",  "name4" : "value4", "name5" : "value5" }

{ "name1" : "value1",  "name2" : "value2", "name3" : "value3",  "name4" : "value4", "name5" : "value5" }.....

etc for 10 times,  or would I do this with a name for my 'database' and make an array of the records:

{ databaseName [

{ "name1" : "value1",  "name2" : "value2", "name3" : "value3",  "name4" : "value4", "name5" : "value5" }

{ "name1" : "value1",  "name2" : "value2", "name3" : "value3",  "name4" : "value4", "name5" : "value5" }

{ "name1" : "value1",  "name2" : "value2", "name3" : "value3",  "name4" : "value4", "name5" : "value5" }

{ "name1" : "value1",  "name2" : "value2", "name3" : "value3",  "name4" : "value4", "name5" : "value5" } .......etc to 10 records as an array

]

}

I think I'm understanding it as the second example - is this correct or am I missing something?  Would it be all right to put 500 records in an array?    I don't know how often they update the w3 website, but maybe they could emphasize this better.   I know they went into creating the json file from a database using php and some other methods, but I  want to create it myself and then upload the file to the server.

One other question is that in the tutorial it said a json file has a json file type, but then they used a file in the example with a .txt type.   I know json files are text files; but can you use .txt on your json file?

 

Share this post


Link to post
Share on other sites

Hi,  i am sending along a copy of a JSON object  that I created from a PHP array using a PHP method called  json_encode().  This is what it looks like.

[	
	{"podcast_no_item":54,"item_title":"Experiment Two","item_description":"Test","item_pubdate":"2017-06-18 10:31:48"},
	{"podcast_no_item":54,"item_title":"Experiment Two","item_description":"Test","item_pubdate":"2017-06-18 10:31:48"},
	{"podcast_no_item":55,"item_title":"Experiment Three","item_description":"Test","item_pubdate":"2017-06-18 11:10:56"},
	{"podcast_no_item":55,"item_title":"Experiment Three","item_description":"Test","item_pubdate":"2017-06-18 11:10:57"},
	{"podcast_no_item":55,"item_title":"Experiment Three","item_description":"Test","item_pubdate":"2017-06-19 08:43:49"},
	{"podcast_no_item":55,"item_title":"Experiment Three","item_description":"Test","item_pubdate":"2017-06-19 08:43:49"},
	{"podcast_no_item":57,"item_title":"Eureka_3","item_description":"I have found it.","item_pubdate":"2017-09-24 11:03:50"},
	{"podcast_no_item":58,"item_title":"Is it ready?","item_description":"It sure looks like it.","item_pubdate":"2017-06-19 15:44:59"},
	{"podcast_no_item":58,"item_title":"Is it ready?","item_description":"It sure looks like it.","item_pubdate":"2017-06-19 17:21:11"}
]

I have included spaces and tabs so that it is easy for you to read.  You can think of a JSON object as one very long array element whose sub-elements consist of a series of objects with property-value pairs.  For example, the above JSON object contains nine objects with four property-value pairs each.   

I am not at all sure that the extension on your file is important.  I have seen JSON contained in several different file formats.

In any case, make sure that you do not place a comma after the last object in the array element.

Also, if you would like to assign the object to a variable as a string so that you can pass it with a $_GET or $_POST  HTTP request, make sure that you enclose it in quotation marks and understand that your JSON object is now a JSON string and must be converted back to an object after it has been sent and received.

Sometimes this is done automatically.  Sometimes it is not in which case you can use JSON.parse().

Roddy

Edited by iwato

Share this post


Link to post
Share on other sites

Yes, you would use an array of objects as shown.  As far as file types, it's not very common to have a static file only containing JSON that you read from, they just do that because it's an easy example.  It's more likely that you'll be sending ajax requests to PHP or another language on the backend which would dynamically generate the JSON (from a database, as in your example).  You can use PHP or whatever language to set the content-type for JSON though, rather than saying it's HTML.

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×