jmarais
jmarais

Reputation: 93

JSON data an object or is it an array, how to check?

I am using jQuery widgets in a Joomla application. The data for the widget must an object and in json format. I am able to achieve that when I retrieve the data direct from the database without the database functionality of the Joomla framework.

Of course, I do want to do it within the Joomla MVC structure and I want to utilise the Joomla database functions. When doing it there is no output in the widget.

I had this before that when passed as an array, it does not display the data, when passed as an object, it does.

My problem is this: Generating the data directly from the database and using the Joomla database structure produces exactly the same result when echoed to the screen but one is displayed in the grid and the other isn't. That was also the case with the previous problem which I had but in that case I called json_encode twice on the same data but there was absolutely no difference in the displayed data, but one works and the other doesn't.

My question: I am not that familiar with JSON data yet but is there a way to check whether the data is represented as an object or an array? This maybe a very dumb question but it seems to me that there must be a difference in the data and by simply looking at it, you are not able to see it, or am I missing something?

UPDATE: Thanks for the responses. I have sat on this for days!! It is working now with the Joomla database functions, it seems there was a minor issue with my code apart from the json encoding but they looked exactly like this when I used the json_encode both in the model and the controller before, therefore double json encoding it, which did not work but when generated outside of Joomla, it worked (single use of json_encoding). Looking at them generated outside of Joomla and after a double json encoding, both of the looked the same, therefore I could not found the problem looking at the output, the same as now.

Thanks for the info regarding the [] and {}, that is insightful but here is an example of my code:

   [{"TotalRows":2,"Rows":[{"login_id":"122","cust_id":"0","shop_id":"0","nickname":null,"shopicon":null,"website":null,"shopname":null, "username":"","password":"","dob":"0000-00-00","comments":null},
{"login_id":"25","cust_id":"57","shop_id":"42","nickname":"qwerty","shopicon":"shop.ico","website":"http:\/\/www.shop.co.uk","shopname":"Shop","username":"eqweq","password":"wqewqeq","dob":"1981-12-14","comments":"qwqeqeqw"}]}]

How can I tell from this whether it is an array or an object?

Upvotes: 1

Views: 929

Answers (4)

Yogendra Joshi
Yogendra Joshi

Reputation: 228

The following code use to get all keys and checks where associated values are in form of array or json object or string value.

Iterator<?> rootIter = jsonData.keys();
while (rootIter.hasNext())
{
   String name = (String) rootIter.next();
   Object obj = jsonData.get(name);
   if (obj instanceof JSONObject)
   {
      JSONObject jsonObj = (JSONObject) obj;
   }
   else if(obj instanceof JSONArray)
   {
       JSONArray jsonArray = (JSONArray) obj;
   }
   else
   {
      String values = obj.toString();
   }           
}

Upvotes: 3

DavidHyogo
DavidHyogo

Reputation: 2896

Sorry user1154041, this is not exactly an answer, but it might help you see the wood for the trees. When developers get a piece of JavaScript (or the subset JSON) ready for deployment they "minify" it, which means to remove all whitespace. It makes no difference to the JavaScript engine, but it's hard for us humans to read. Re-formatting a minified piece of code is sometimes called 'prettify' and the process used to need special tools. These days all the editors I use have a 'format' function which takes a string of JavaScript or JSON like your example and formats it in a much more readable form like the example below (I had to add the var statement to make it valid JavaScript so that my editor understood what to do):

var test = [{
    "TotalRows": 2,
    "Rows": [{
        "login_id": "122",
        "cust_id": "0",
        "shop_id": "0",
        "nickname": null,
        "shopicon": null,
        "website": null,
        "shopname": null,
        "username": "",
        "password": "",
        "dob": "0000-00-00",
        "comments": null
    }, {
        "login_id": "25",
        "cust_id": "57",
        "shop_id": "42",
        "nickname": "qwerty",
        "shopicon": "shop.ico",
        "website": "http://www.shop.co.uk",
        "shopname": "Shop",
        "username": "eqweq",
        "password": "wqewqeq",
        "dob": "1981-12-14",
        "comments": "qwqeqeqw"
    }]
}]

I know you still have to get used to the fact that [] indicates an array and {} an object, but don't you agree it's much easier to see? So, when you're puzzled by a string like that, open up an IDE like Aptana http://www.aptana.com, designed for editing JavaScript. Create a JavaScript file, assign the string to a var and hit the format button. That way you have half a chance of figuring out the structure of the code.

Upvotes: 0

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22441

If you have JSON string, you can simply check if first non-whitespace symbol is { or [. { would mean that top level is regular object (remember that arrays are objects too!), [ - array. You can also parse it to object and check if instanceof Array is true for the result.

Upvotes: 1

phenomnomnominal
phenomnomnominal

Reputation: 5515

They should be very different:

var arr = [1, 2, 3, 4];
console.log(JSON.stringify(arr)); // prints "[1,2,3,4]"
var obj = { a: 1, b: 2, c: 3, d: 4 };
console.log(JSON.stringify(obj)); // prints "{"a":1,"b":2,"c":3,"d":4}".

Can you please show some inputs and outputs (and some code) so we can see what is happening?

Upvotes: 0

Related Questions