0

I get the following struct return from disqus.com API and I just don't know how to retrieve only the following "id" value in red using Coldfusion.

enter image description here

This is the full array returned.

 {
  "cursor":{
    "prev":null,
    "hasNext":false,
    "next":"1213061503000000:1:0",
    "hasPrev":false,
    "total":null,
    "id":"1213061503000000:1:0",
    "more":false
  },
  "code":0,
  "response":[
    {
      "category":"1",
      "reactions":0,
      "identifiers":[],
      "forum":"bobross",
      "title":"Donkeys live a long time",
      "dislikes":0,
      "isDeleted":false,
      "author":"1",
      "userScore":0,
      "id":"2",
      "isClosed":false,
      "posts":0,
      "link":null,
      "likes":0,
      "message":"\"Donkeys live a long time. None of you have ever seen a dead donkey.\"",
      "ipAddress":"127.0.0.1",
      "slug":"donkeys_live_a_long_time",
      "createdAt":"2008-06-10T02:31:43"
    },
    {
      "category":"1",
      "reactions":0,
      "identifiers":[
        "my-identifier"
      ],
      "forum":"bobross",
      "title":"Happy Accidents",
      "dislikes":0,
      "isDeleted":false,
      "author":"1",
      "userScore":0,
      "id":"1",
      "isClosed":false,
      "posts":76,
      "link":null,
      "likes":0,
      "message":"\"If you've painted before you know that we don't make mistakes -- we have happy accidents.\"",
      "ipAddress":"127.0.0.1",
      "slug":"happy_accidents",
      "createdAt":"2008-06-10T01:31:43"
    }
  ]
}
1
  • I would love too but I have nothing to show you since I don't have any idea how to achieve that. Commented Aug 31, 2013 at 17:50

1 Answer 1

6

Well: firstly, that's a JSON packet not an array, so you need to turn it into a CFML data structure by deserialising it, eg:

data = deserializeJson(jsonPacket);

Then you'll have a native CFML struct (not an array... the array is one of the values within the struct).

From there, you would access any given element the way one normally would with a CFML struct, using struct / array notation, or struct functions / etc.

To directly address the item you point out, it would be (given the code above has been run first):

data.response[1].id

However I suspect you don't really want to address just that one value? But without more detail as to what you're trying to do, it's difficult to answer other than exactly what you want.

If you wanted to get all the IDs, one could do this:

ids = [];
for (singleResponse in data.response){
    arrayAppend(ids, singleResponse.id);
}

Or on ColdFusion 10 there's more options with how to iterate over arrays.

Again: clarify what you're trying to do, and we can help you do it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a millions time ... So well explained... I need to get the id value for when the title from the response = "Donkeys live a long time"
Its ok I found the solution thanks to your help <cfloop array="#data.response#" index="struct"> <cfif struct.isClosed EQ "false"> <cfset threadid = struct.id> </cfif> </cfloop>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.