Trying to identify the type of array, I have an multidimensional JSON array that i pull from a website, but its not using keypairs i am farmiliar with. The structure is as follows. I have only included 1 array item to show what it's doing.
"o": {
"ah": ["id1", "12", "id2", "32", "id4", "4", "id5, "6"]
},
My searches on both JavaScript and JSON objects and strings always use a semi-colon : to define the key and the value.
In the end I just want to loop through the multiple items and print them out.
To provide more clarification about the array structure:
{
"outer": {
"item1":[ {
"c": {
"k": 26862, "n": "theName"
},
"o": {
"ah": ["id1", "0", "id2", "0", "id3", "0.98", "id4", "0.94", "id5", "5", "id6", "-0/0.5"],
"ou": ["id7", "3.5", "id8", "3.5", "id9", "1.53", "id10", "0.55", "id11", "3.4", "id12"],
"1x2": ["id13", "1.20", "id14", "25.00", "id15", "4.80"]
},
"egn":""
}
]
},
{
"item1":[ {
"c": {
"k": 26862, "n": "theName"
},
"o": {
"ah": ["bd1", "0", "bd2", "0", "bd3", "0.98", "bd4", "0.94", "bd5", "0.5", "bd6", "-0.5"],
"ou": ["bd7", "3.5", "bd8", "3.5", "bd9", "1.53", "bd10", "0.55", "bd11", "3.4", "bd12"],
"1x2": ["bd13", "1.20", "bd14", "25.00", "bd15", "4.80"]
},
"egn":""
}
]
}
}
If I wanted to bind an ID to a specific field e.g. and it display the value of ID 1 in the array structure how would I do it without a keypair value, also if the array ID is not standardized so you can't loop through like var = "id"+ i foreach element?
o is an object, ah is an array which is a property of o object. You use JSON.parse() to convert JSON string to javascript object, then iterate over o.ah using Array.prototype.forEach()
// or however you obtain the JSON string
var obj = JSON.parse(jsonStr);
obj.o.ah.forEach(function(value) {
console.log(value);
});
Related
{
"orderId": "3",
"storeId": "1",
"products": [
"productId": "123",
{
"prescriptions" : [{
"eye": "1",
"sph": "1",
"cyl": "1",
"add": "1",
"axis": "1"
}],
"lensItems": {
"lensId": "1",
"lensType": "1",
"lensCoating": "1",
"index": "1",
"price": "3000"
},
"frameSize": {
"a": "1",
"b": "1",
"ed": "1",
"fh": "1",
"d": "2"
},
"counterSale": false
}
]
}
I want to check the prescriptions only if the lensItems exists.
I have tried this,
body('products.*.prescriptions')
.if(body('products.*.lensItems').exists())
.exists()
.withMessage('prescriptions not exists')
.isArray()
.withMessage('prescriptions is not an array')
.notEmpty()
.withMessage('prescriptions is empty'),
Its working when I check with string like below,
body('products.*.prescriptions')
.if(body('products.*.productId').exists())
.exists()
.withMessage('prescriptions not exists')
.isArray()
.withMessage('prescriptions is not an array')
.notEmpty()
.withMessage('prescriptions is empty'),
I want to the compare array of object with object . If any more regarding this please comment, I will share you much informations which are possible. Thanks in advance
This is the data I'm trying to create for my POST method:
{
"main": {
"b": [{
"field": "ss",
"value": "30",
"operator": "LT"
}],
"a": [{
"field": "tttt",
"value": "s",
"operator": "IN"
}
]
}
}
The below fiddle has the code that I use to get this JSON structure.
http://jsfiddle.net/dz4cq0sj/14/
While looping through an array, it takes only the last object that was created and does not push any previous objects, like in this case I have:
obj.criteria[a,b] //whereaandbare objects, so it takes only the b objects details.
{
"main": {
"b": [{
"field": "ss",
"value": "30",
"operator": "LT"
}],
"a": []
}
}
Any idea how to fix this?
Thanks!
I'm trying to sort the json below, but until now couldn't find the way. I get this json by jQuery ajax with datatype json. How can I apply sort to this keeping the index? Every time i try to sort javascript throws an error telling undefined is not a function. I already know that json or objects don't have sort.
So how can I achieve this? I really need to keep this index.
{
"header": {
"user": "1059",
"authenticated": true,
"isGuest": false,
"time": 1416362117
},
"batches": {
"3503": { // Preserve this index
"user": "1059",
"auction": "1826",
"number": "1",
"published": "1",
"type": "1",
"status": "1",
"date_start": "1415379600",
"date_end": "1417021200", // Sort by this value
},
"3583": {
"user": "1059",
"auction": "1886",
"auto_value": "0.00",
"number": "1",
"published": "1",
"type": "1",
"status": "1",
"date_start": "1415376000",
"date_end": "1417017600",
},
}
}
As I wrote and cited in comments - properties do not have an order. Make an array of single-property objects, which you can sort. Something like this:
var data = {
"header": {
"user": "1059",
"authenticated": true,
"isGuest": false,
"time": 1416362117
},
"batches": [ // this is an array of objects, not a single object
{"3503": { // Preserve this index
"user": "1059",
"auction": "1826",
"number": "1",
"published": "1",
"type": "1",
"status": "1",
"date_start": "1415379600",
"date_end": "1417021200", // Sort by this value
}},
{"3583": {
"user": "1059",
"auction": "1886",
"auto_value": "0.00",
"number": "1",
"published": "1",
"type": "1",
"status": "1",
"date_start": "1415376000",
"date_end": "1417017600",
}}
] // end of array
}
data.batches.sort(function(a, b) {
return a[Object.keys(a)[0]].date_end - b[Object.keys(b)[0]].date_end;
});
console.log(data.batches);
And you can keep those numbers inside of the object containing user, auction and so on for easier access.
How can I concat JSON objects if the first object is:
{
"total": "2"
}
And second:
[
"player1": {
"score": "100",
"ping": "50"
},
"player2": {
"score": "100",
"ping": "50"
}
]
If I want final result to look like:
{
"total": "2",
{
"player1": {
"score": "100",
"ping": "50"
},
"player2": {
"score": "100",
"ping": "50"
}
}
}
I am trying to do it in JavaScript.
Update
The final result is not a valid array.
What about this?
{
[
"player1": {
"score": "100",
"ping": "50"
},
"player2": {
"score": "100",
"ping": "50"
}
]
}
Your json is not valid. If you want something valid, you need to do something like
{
"total": "2",
"players": [
"player1": {
"score": "100",
"ping": "50"
},
"player2": {
"score": "100",
"ping": "50"
}
]
}
As I've said in the comments,
you're trying to append an invalid Array getting as a result an invalid JSON tree.
To validate an expected JSON you can test it here: http://jsonlint.com/
What's wrong 1:
[ "player1" : { "score": "100", "ping": "50"} ] // Array ??
^ invalid delimiter (should be ,)
What's wrong 2 in the expected result:
{
"property" : "value" ,
"total" : "2" ,
{ "player1" : { "score": "100", "ping": "50" }
^^^^^ where's the property here???
}
What you can do:
var myObject = {"total":"2"};
var players = {
"player1": {
"score": "100",
"ping": "50"
},
"player2": {
"score": "100",
"ping": "50"
}
};
myObject.players = players;
console.log( myObject );
which will result in:
[object Object] {
players: [object Object] {
player1: [object Object] { ... },
player2: [object Object] { ... }
},
total: "2"
}
Object Literal needs a comma separated PROPERTY : VALUE pair model
{
"property1" : val,
"property2" : [],
"property3" : "String",
"property4" : {}
}
The above is a valid JSON cause implies that your property names are wrapped in double quotes " ".
"property" : value
Array (as seen in your Question) cannot be separated by : but with ,
myArr = ["string", {}, value]; // is a valid Array.
with object:
myArr = ["string", {"objectProperty":"val"}, value];
Now to get a key value out of Array:
myArr[0] // "string"
myArr[1] // {object Object}
myArr[1].objectProperty // "val" // get Object property value
So as seen from above Arrays are values stored in numbered keys (0 zero based), while Objects are property - value pairs. What they have in common is the comma (,) delimiter.
Your example isn't a correct JSON object, you should do it like this
var x = { "total": "2" }
x.players = playerArray
Am having a json like below,
[
{
"id": "1",
"freq": "1",
"value": "Tiruchengode",
"label": "Tiruchengode"
},
{
"id": "2",
"freq": "1",
"value": "Coimbatore",
"label": "Coimbatore"
},
{
"id": "3",
"freq": "1",
"value": "Erode",
"label": "Erode"
},
{
"id": "4",
"freq": "1",
"value": "Madurai",
"label": "Madurai"
},
{
"id": "5",
"freq": "1",
"value": "Salem",
"label": "Salem"
},
{
"id": "6",
"freq": "1",
"value": "Tiruchirappalli",
"label": "Tiruchirappalli"
},
{
"id": "7",
"freq": "1",
"value": "Tirunelveli",
"label": "Tirunelveli"
}
]
I need to pattern match it with label item in this json (ie), If I type tiru, then it has to result label items having tiru substrings in it.If its a single item array I know how to pattern match and sort it. Here am completely unaware that, how to pattern match using label item in the array. Is it possible to?. I need to do with Pure javascript, any help guys?
You can use the functional array methods introduced in JavaScript 1.6, specifically filter:
var search = 'tiru';
var results = obj.filter(function(item) {
var a = item.label.toUpperCase();
var b = search.toUpperCase();
return a.indexOf(b) >= 0;
});
If you wanted labels only, you can then use map to return only that property alone:
var labels = obj.filter(function(item) {
var a = item.label.toUpperCase();
var b = search.toUpperCase();
return a.indexOf(b) >= 0;
}).map(function(item) {
return item.label;
});
Essentially, filter is a method available to any Array which returns a new Array containing only those members for which the supplied function return true.
JSON.parse() will help convert the jsonString to JsonObject then just iterate the object use indexOf for pattern matching.
var jsonString = '[{"id": "1","freq": "1","value": "Tiruchengode","label": "Tiruchengode"},{"id": "2","freq": "1","value": "Coimbatore","label": "Coimbatore"},{"id": "3","freq": "1","value": "Erode","label": "Erode"},{"id": "4","freq": "1","value": "Madurai","label": "Madurai"},{"id": "5","freq": "1","value": "Salem","label": "Salem"},{"id": "6","freq": "1","value": "Tiruchirappalli","label": "Tiruchirappalli"},{"id": "7","freq": "1","value": "Tirunelveli","label": "Tirunelveli"}]';
var jsonObjects = JSON.parse(jsonString);
var pattern = "tiru";
for(var key in jsonObjects){
var label = jsonObjects[key].label.toUpperCase();
if(label.indexOf(pattern.toUpperCase()) != -1){
document.write(label+"<br/>");
}
}