Forum Discussion

perk's avatar
perk
Adept
10 years ago

Getting JSON fields in Freemarker

Hi,

 

I have code that retrieves a JSON object with the following structure and stores it in a variable named response_getUser:

 

{
   "user": {
      "id": 123
}
}

However, I am unable to access the id using the following code

 

response_getUser.content.user.get("id")

I keep receiving this a null error for the code.

 

Is there any help on how to debug this? I am new to using freemarker and it has been really difficult to figure out what the variables are in lithium.

 

 

  • DougS's avatar
    DougS
    Khoros Oracle
    you should be able to do one of theses two (I'm afk, or would have tested it first):

    <#assign respons_getUser = { "user": { "id": 123 } } />
    ${user.id}

    OR:

    <#assign response_getUser = "{ "user": { "id": 123 } }"?eval />
    ${user.id}

    I'd recommend the first way over the second, as using eval can be dangerous - you need to be careful to avoid freemarker injection attacks.