I want to access over an array object in JSON by index with a variable. Consider the following code:
declare @pjson nvarchar(max)='{
"store":{
"storeId": 100,
"name": "TEST",
"lastUpdatedBy": "MULE",
"location": {
declare @pjson nvarchar(max)='{
"store":{
"storeId": 100,
"name": "TEST",
"lastUpdatedBy": "MULE",
"location": {
"addresses": [
{
"addressType": "MAIN",
"name": "Name1",
"name2": "Name2",
"address": "Address1",
"address2": "Address2",
"city": "City",
"lastUpdateBy": "MULE"
},
{
"addressType": "SECONDARY",
"name": "Name1",
"name2": "Name2",
"address": "Address1",
"address2": "Address2",
"city": "City",
"lastUpdateBy": "MULE"
},
{
"addressType": "BILLING",
"name": "Name1",
"name2": "Name2",
"address": "Address1",
"address2": "Address2",
"city": "City",
"lastUpdateBy": "MULE"
}
]
}
}
}'
Declare @counter1 INT = 0;
Print JSON_VALUE(@pjson,N'lax $.store.location.addresses[@counter1].addressType')
I get an error:
JSON path is not properly formatted. Unexpected character '@' is found at position 31.
If I try directly by passing number as
Declare @counter1 INT = 0;
Print JSON_VALUE(@pjson,N'lax $.store.location.addresses[0].addressType')
I get the expected result
MAIN
Is there something that I am missing while passing the variable?
pathparameter. I don't think that you can use@counterdirectly as part of thepathparameter. Anddeclare @pjson nvarchar(max)='part of the test JSON is probably a typing error.