0

I have the following JSON data:

{
        "searchResults": [
            {
                "self": "https://api.icims.com/customers/xxxx/applicantworkflows/689997",
                "id": 689997
            },
            {
                "self": "https://api.icims.com/customers/xxxx/applicantworkflows/691005",
                "id": 691005
            }
        ]
    }

I need to get the value of "id": 691005

I thought I could do something like this:

    <script>
    var myJson = ({
        "searchResults": [
            {
                "self": "https://api.icims.com/customers/xxxx/applicantworkflows/689997",
                "id": 689997
            },
            {
                "self": "https://api.icims.com/customers/xxxx/applicantworkflows/691005",
                "id": 691005
            }
        ]
    })

    myArray = JSON.parse(myJson);
    console.log(myArray[1]);
</script>

throws an error.

How can I get the value "691005" from the data using only JS not libraries?

2
  • 1
    The thing you have posted it not JSON, it's an object. You can tell because the "an error" it throws tells you: "JSON.parse: unexpected character at line 1 column 2 of the JSON data". Also, why wrapping with ()? Commented Jul 13, 2021 at 21:32
  • JSON.parse stand for string content, and produce a JSON data. your var myJson is already a JSON data Commented Jul 13, 2021 at 21:36

3 Answers 3

2

JSON.parse() is used to produce a JSON from a string, not from another JSON.

Instead, simply get the id property from the second item in the searchResults property.

const myJson = {
  "searchResults": [{
      "self": "https://api.icims.com/customers/xxxx/applicantworkflows/689997",
      "id": 689997
    },
    {
      "self": "https://api.icims.com/customers/xxxx/applicantworkflows/691005",
      "id": 691005
    }
  ]
}


const id = myJson.searchResults[1].id;
console.log(id);

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

Comments

0

You could also find the item by the id, instead of the index of the array, like that

var myJson = `
{
  "searchResults": [
      {
          "self": "https://api.icims.com/customers/xxxx/applicantworkflows/689997",
          "id": 689997
      },
      {
          "self": "https://api.icims.com/customers/xxxx/applicantworkflows/691005",
          "id": 691005
      }
  ]
}
`

const myArray = JSON.parse(myJson);
const selectedItem = myArray.searchResults.find((item) => {
  return item.id === 689997
});

console.log(selectedItem)

Comments

0

You can access it directly:

<script>
    var myJson = ({
        "searchResults": [
            {
                "self": "https://api.icims.com/customers/xxxx/applicantworkflows/689997",
                "id": 689997
            },
            {
                "self": "https://api.icims.com/customers/xxxx/applicantworkflows/691005",
                "id": 691005
            }
        ]
    })

    console.log(myJson['searchResults'][0]['id']);

</script>

This code will output: 691005.

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.