How to Get the Index from a Json Object With Value

How can I get the index from a JSON object with value?

You will have to use Array.find or Array.filter or Array.forEach.

Since your value is array and you need the position of the element, you will have to iterate over it.

Array.find

var data = [{"name":"placeHolder","section":"right"},{"name":"Overview","section":"left"},{"name":"ByFunction","section":"left"},{"name":"Time","section":"left"},{"name":"allFit","section":"left"},{"name":"allbMatches","section":"left"},{"name":"allOffers","section":"left"},{"name":"allInterests","section":"left"},{"name":"allResponses","section":"left"},{"name":"divChanged","section":"right"}];
var index = -1;
var val = "allInterests"
var filteredObj = data.find(function(item, i){
if(item.name === val){
index = i;
return i;
}
});

console.log(index, filteredObj);

How to write a select query to get the index value from Json object

If I understand the question correctly and you want to get the index of the items in the Object JSON array, you need to use OPENJSON() with default schema. The result is a table with columns key, value and type and in case of JSON array, the key column holds the index of each item in the array (0-based):

JSON:

DECLARE @json nvarchar(max) = N'{
"Model":[
{
"ModelName":"Test Model",
"Object":[
{
"ID":1,
"Name":"ABC"
},
{
"ID":11,
"Name":"ABCD"
},
{
"ID":15,
"Name":"ABCDE"
}
]
}
]
}'

Statement:

SELECT CONVERT(int, j2.[key]) + 1 AS item_id
FROM OPENJSON (@json, '$.Model') j1
CROSS APPLY OPENJSON(j1.[value], '$.Object') j2

But if you want to get the values of the ID keys in the Object JSON array, the statement is different:

SELECT j2.ID
FROM OPENJSON (@json, '$.Model') j1
CROSS APPLY OPENJSON(j1.[value], '$.Object') WITH (
ID int '$.ID'
) j2

Note, that you need two OPENJSON() calls, because the input JSON has nested array structure. Of course, if Model JSON array has always one item, you may simplify the statement using an appropriate path:

SELECT CONVERT(int, [key]) + 1 AS item_id
FROM OPENJSON (@json, '$.Model[0].Object')

Finally, to get index, ID and Name, you should use the following statement, which assumes, that $.Model JSON array has more than one item and defines ID and Name columns with the appropraite data types:

SELECT 
CONVERT(int, j2.[key]) + 1 AS ItemID,
j3.ID, j3.Name
FROM OPENJSON (@json, '$.Model') j1
CROSS APPLY OPENJSON(j1.[value], '$.Object') j2
CROSS APPLY OPENJSON(j2.[value], '$') WITH (
ID int '$.ID',
Name varchar(50) '$.Name'
) j3

get json key and value by index instead of their names

You can transform the values into an array with Object.values, and then get the index you want:

var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}')
console.log(obj.name) // John
var array = Object.values(obj)
console.log(array[0]) // John

how to get value from json object with its index number in reactjs like posts[2].title

Remove the map function and simply write

<li>
<span>{this.state.posts[0].title}</span>
</li>

Get the index of certain value of JSON Objects

if you want to get the index of the particular item by ID then first of all you have to get the item object by ID as below and then you can use indexOf to get the index of that particular item.

var item = yourArray.find(function (e) {
if (e.id == "your Id") {
return e;
}
});

var index = yourArray.indexOf(item);

How do I index an item in a JSON array from Python

Lets say you have your json in a dictionary format. If not you could use some library to convert to a dict (the json library does that)

then I would iterate over the dict and check if each element has the key, in the example "person2", if yes, we found the element and can print the index.

Here is the code:

i = 0
for person in json["users"]:
if "person2" in person:
print(i)
i += 1

There is probably a much cleaner solution, but that is what I can think of.

javascript json How to get index of key in json array

You can use ES2015 array method:

jsonArray.findIndex(item => Object.keys(item).includes("key3"));


Related Topics



Leave a reply



Submit