Hi not sure if this is a suitescript or its just m...
# suitescript
t
Hi not sure if this is a suitescript or its just me not doing this correct accessing the object. But I have this JSON in my config const dataFromServer = "lcsMapping":"'{\"FULL_LICENSE_USERS\":{\"1\":{\"MIN\":16,\"MAX\":74},\"2\":{\"MIN\": 75,\"MAX\":150},\"3\":{\"MIN\":151,\"MAX\":299},\"4\":{\"MIN\":300,\"MAX\":300}}}'"}; and when I try to access this on the backend dataFromServer.lcsMapping['FULL_LICENSE_USERS'] I've got an undefined 🤔 not sure what is my mistake on this
r
try using Object.keys() for each key keys = Object.keys(dataFromServer) keys = Object.keys(dataFromServer["lcsMapping"]) and go further.
t
Tried it
console.*log*(*Object*.*keys*(dataFromServer));
console.*log*(*Object*.*keys*(dataFromServer["lcsMapping"]));
then here is the result on the image 🤔
my expectation is I should get the \"1\":{\"MIN\":16,\"MAX\":74}
r
Just to explain to you what is happening is: datastring=dataFromServer["lcsMapping"] the value is a string and not an object. Which is why you are seeing all the indexes of the string. What you can do is something like this. data = datastring.substring(1, datastring.length-1) dataobj = JSON.parse(data) then you can access keys which are 1,2,3,4 mykeys = Object.keys(dataobj)
message has been deleted
s
is the code above copied exactly? that does not even pass as valid Javascript, and the code to the right of the assignment operator is not a valid string, let alone valid JSON. the first and last characters of a JSON string must always be opening and closing square or curly brackets. A valid JSON is a single string, with double-quoted keys and string values
and also, even if it was a valid JSON string, you would first need to parse it as @raghav has said above
t
hi @scottvonduhn, yea I already format the JSON into something like that, but I still got the index instead.
s
what did you format it into, then? the code above is not right
r
you are getting a string and not a JSON/object. To parse the string into JSON, You can use substring to remove the tilt <`> character from your string which is at the start and at the end, then you need to parse it and this will give you an object. Using the code above will work, but I am not sure how you got this JSON in the first place. it is not correct. You might want to correct something at the source itself.
s
It’s definitely not valid JSON, and not valid Javascript either. I’d strongly suggest using static code analysis, like ESLint, to check your code for typos. It would immediately alert you to the fact that this is broken.
t
The source of the JSON is actually correct. I even use JSONFormatter to identify if its really valid. But we have this design that we want to pass the data from server to client. Then the JSON format disrupts. Actually if I hard coded the JSON then access it I have no problem
s
this is a valid JSON string:
'{"FULL_LICENSE_USERS":{"1":{"MIN":16,"MAX":74},"2":{"MIN":75,"MAX":150},"3":{"MIN":151,"MAX":299},"4":{"MIN":300,"MAX":300}}}'
note that it starts with a curly brace, and ends with one
this is also valid JSON:
'{"lcsMapping":{"FULL_LICENSE_USERS":{"1":{"MIN":16,"MAX":74},"2":{"MIN":75,"MAX":150},"3":{"MIN":151,"MAX":299},"4":{"MIN":300,"MAX":300}}}}'
t
thanks @scottvonduhn and @raghav 🙂