0

so i would like to list the first value of users json i get from making an rest api call with javascript so that the response is like this :

"XXX-XXX", "DDD-DDD", "KKK-KKK", "UUU-UUU"

Json Data from API call:

   users:{
   "XXX-XXX":{
      "Info":{
         "ID":"08",
         "Created": "2021-07-10",
         "Plan": "Basic"}},
   "DDD-DDD":{
       "Info":{
          "ID":"04",
          "Created": "2021-07-11",
          "Plan": "Prime"}}
      },
   "KKK-KKK":{
       "Info":{
          "ID":"02",
          "Created": "2021-07-11",
          "Plan": "Prime"}}
      },
   "UU-UUU":{
       "Info":{
          "ID":"13",
          "Created": "2021-07-11",
          "Plan": "Prime"}}
      }
    }
2
  • 3
    Object.keys(users). Commented Jul 19, 2021 at 11:35
  • 1
    Is that the format coming from the API? if you check it there's some error in terms of formatting there's some exceeded curly braces. you need to format it properly first then use Object.keys(data) Commented Jul 19, 2021 at 11:43

4 Answers 4

1

With Object.keys() you will get the result that you want into an array.

let users = {
    "XXX-XXX": {
        "Info": {
            "ID": "08",
            "Created": "2021-07-10",
            "Plan": "Basic"
        }
    },
    "DDD-DDD": {
        "Info": {
            "ID": "04",
            "Created": "2021-07-11",
            "Plan": "Prime"
        }
    },
    "KKK-KKK": {
        "Info": {
            "ID": "02",
            "Created": "2021-07-11",
            "Plan": "Prime"
        }
    },
    "UU-UUU": {
        "Info": {
            "ID": "13",
            "Created": "2021-07-11",
            "Plan": "Prime"
        }
    }
}

let result = Object.keys(users)
console.log(result)

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

Comments

1

//to get the attributes as array:

Object.keys(your_json_object)

Comments

1

Do this users[Object.keys(users)[0]]

Comments

1
const users = {
    "XXX-XXX": {
        "Info": {
            "ID": "08",
            "Created": "2021-07-10",
            "Plan": "Basic"
        }
    },
    "DDD-DDD": {
        "Info": {
            "ID": "04",
            "Created": "2021-07-11",
            "Plan": "Prime"
        }
    },
    "KKK-KKK": {
        "Info": {
            "ID": "02",
            "Created": "2021-07-11",
            "Plan": "Prime"
        }
    },
    "UU-UUU": {
        "Info": {
            "ID": "13",
            "Created": "2021-07-11",
            "Plan": "Prime"
        }
    }
}

console.log('"' + Object.keys(users).join('", "') + '"');

Comments

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.