JSON.parse() returns alphabetical order - json

I'm parsing my string value to json object.
while doing this it returns the json object with different order.
var l = '{"creationType":"ITEMCLASSES","maxLevelNo":"4","selectbox1":"11001","textbox11":"KRAFT PAPER","hidden11":"11001","textbox12":"FG2","hidden12":"11051","textbox13":"HMC2","hidden13":"11045","textbox14":"2","textbox15":"LS2","hidden15":"11048","textbox16":"123","hidden16":"11015","selectbox2":"11002","textbox21":"kraft ","hidden21":"11057","selectbox3":"11003","textbox31":"40 BF","hidden31":"11004","textbox32":"Natural Color","hidden32":"11006","textbox33":"2","textbox41":"140 GSM","hidden41":"11008"}';
var o = JSON.parse(l);
alert(0);
resultant object is ordered based on the name.
how to avoid this situation?
kindly give your suggestion.

Your variable "1" is already a JSON object. This means that feeding it into the JSON.parse function is both redundant and unnescesarry. The JSON.parse(); function is primarily used when converting strings to JSON objects.
var l = {"creationType":"ITEMCLASSES","maxLevelNo":"4","selectbox1":"11001","textbox11":"KRAFT PAPER","hidden11":"11001","textbox12":"FG2","hidden12":"11051","textbox13":"HMC2","hidden13":"11045","textbox14":"2","textbox15":"LS2","hidden15":"11048","textbox16":"123","hidden16":"11015","selectbox2":"11002","textbox21":"kraft ","hidden21":"11057","selectbox3":"11003","textbox31":"40 BF","hidden31":"11004","textbox32":"Natural Color","hidden32":"11006","textbox33":"2","textbox41":"140 GSM","hidden41":"11008"};
alert(1);
Just skip the second line and manipulate your "1" variable directly.

You can't.
As described by www.json.org
In JSON, they take on these forms:
An object is an unordered set of name/value pairs
Use an array if you want to maintain the order.

Related

Ambiguous format output in nodejs

I am having output in following format as
"[{"a":"a1"},{"a":"a2"}]"
I want to actually extract it in array of json:
[
{
"a":"a1"
},
{
"a":"a2"
}
]
How to convert it?
You have tagged this with Node-RED - so my answer assumes that is the environment you are working in.
If you are passing a message to the Debug node and that is what you see in the Debug sidebar, that indicates your msg.payload is a String with the contents of [{"a":"a1"},{"a":"a2"}] - the Debug sidebar doesn't escape quotes when displaying strings like that.
So you likely already have exactly what you want - it just depends what you want to do with it next.
If you want to access the contents you need to parse it to a JavaScript Object. You can do this by passing your message through a JSON node.
Assuming your input contains the double quotes in the beginning and end, it is not possible to directly JSON.parse() the string.
In your case, you need to remove the first and last character (the double quotes) from your string before parsing it.
const unformattedString = '"[{"a":"a1"},{"a":"a2"}]"'
const formattedString = unformattedString.substr(1, unformattedString.length - 2)
const json = JSON.parse(formattedString)
The variable json now contains your JSON object.
I would suggest a different method which will get your work done without using any third party library.
var a = '[{"a":"a1"},{"a":"a2"}]';
var b = JSON.parse(a);
console.log(b); // b will return [{"a":"a1"},{"a":"a2"}]
Another way which is eval function which is generally not recommended
var d = eval(a);
If you want to use JQuery instead use :
var c = $.parseJSON(a);

How to get the length of object within a JSON?

How do I get the length of an object stored within a JSON?
Object.keys can be used to return an array of all the keys of the object and then you get the length of that array - that's the number that you are looking for.
console.log( Object.keys({"0":89,"1":54,"2":34,"3":67,"4":131}).length );

parsing JSON on appcelerator

i have a webservice like this:
{"person":{"name account":"Jhon Doe","Image":"image/test","Adress":"New York 43","Recomendations":"40"}}
this is what i'm trying to do, when i print datos i get the whole json but when i try to print just the name or Image i don't get anything
var urll = "example.com/example";
var json;
var xhrr = Ti.Network.createHTTPClient({
onload: function() {
json = JSON.parse(this.responseText);
var datos = JSON.stringify(json);
var medicos = datos;
Ti.API.info("Json! "+datos);
}
});
xhrr.open('GET', urll);
xhrr.send();
i tried with datos[0].person, datos.person but nothing
You have to understand the difference between a string that contains a JSON representation of an object - and the object itself.
Your json variable contains the object (which is what JSON.parse(...) does - convert a text string to an object). On the object you can refer to the attributes as you discovered. You can do this in two ways:
json.person.Address
json.person['name account']
I would suggest that you try to avoid attributes with names that are not valid identifiers (as the latter of the two) as this makes it a little more difficult to use them - e.g. by not allowing the dot notation.
Your datos variable on the other hand contains the string representation of the json object (as JSON.stringify(...) does exactly that - convert an object to its string representation). This means that datos is the same as this.responseText (since you first parse it and then stringify it back).
So JSON.stringify(...) is a brilliant way to make the object "human readable" but you need the object to work with the data.
Hope this clarifies the terms a little ;-)
/John
i just find the solution:
Ti.API.info("Json! "+json.person.Recomendations);

Get length/size of json data

I have use the following codes to get my json data with certain nodes.
console.log(Object.keys(data.Top["person"]).length);
It is work fine in following structure of json data array:
data:{"Top":{"person":[{"A":"a","B":"b"},{"A":"a","B":"b"}]}}
However when the person only have one nodes, it always show the answer 2 to me, it should be answer 1.
data:{"Top":{"person":{"A":"a","B":"b"}}}
Is it possible to solve this error?
length property is supported by type array.
data:{"Top":{"person":[{"A":"a","B":"b"},{"A":"a","B":"b"}]}} in case of this person is array enclosed with [ ]
Where as for data:{"Top":{"person":{"A":"a","B":"b"}}} person is just an object. Hence length is undefined for it.
If you are creating json out of string or formatting it make sure to include [ and ] for person attribute.
JSFiddle
https://jsfiddle.net/d6pqnckh/
Also use JSON formatter to test JSON structure.
UPDATE
Since you are not sure of JSON structure. What you could do is before accessing length of person check if it is an array or object.
if(Object.prototype.toString.call(data.Top.person) === '[object Array]')
alert(data.Top.person.length);
//It is an array
else
alert("I AM OBJECT"); //It is of an object type
Updated Fiddle: https://jsfiddle.net/wys7awuz/
To make it an array regardless https://jsfiddle.net/ofkboh6k/
var p = data.Top.person;
delete data.Top.person;
data.Top.person = [];
data.Top.person.push(p);
alert(data.Top.person.length);
Include this in else part of condition. It will make it an array.
length works for type array.
change your JSON to
data:{"Top":{"person":[{"A":"a","B":"b"}]}}

trying to convert data from Domino Access Service to a JSON string via JSON.stringify

I want to store the result from a call to a Domino Access Service (DAS) in a localStorage however when I try to convert the result object to a JSON string I get an error.
With DAS you get the result as an Array e.g.:
[
{
"#entryid":"1-CD90722966A36D758025725800726168",
"#noteid":"16B46",
Does anyone know how I get rid of the square brackets or convert the Array quickly to a JSON object?
Here is a snippet of my code:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
localStorage.setItem('myCatalog',JSON.stringify(data));
}
});
Brackets are part of the JSON syntax. They indicate that this is an array of objects. And as you point to a view it is very likely that you would get more than one object back (one for each entry in the view).
So if you are only interested in the first element you could do this:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
var firstRecord = data[0] || {};
localStorage.setItem('myCatalog',JSON.stringify(firstRecord));
}
});
Otherwise, you would need to define a loop to handle each of the objects :-)
/John