What is the process for retrieving a value from JSON utilizing JavaScript?

Unfortunately, I am completely stumped when it comes to Javascript. I'm attempting a few solutions based on online resources. If anyone could offer some assistance, that would be greatly appreciated.

Below is the JSON data provided. It has been condensed to just three items, but in actuality, it could contain up to 50 items. My goal is to extract either the id 7514 if the customFieldId is 3 or the id 3854 if the customFieldId is 1.

{
"items": [
    {
        "id": 3854,
        "customFieldId": 1,
        "customField": {
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/1"
                }
            ]
        },
        "value": "Yes",
        "links": [
            {
                "rel": "canonical",
                "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/3854"
            }
        ]
    },
    {
        "id": 7514,
        "customFieldId": 3,
        "customField": {
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/3"
                }
            ]
        },
        "value": "No",
        "links": [
            {
                "rel": "canonical",
                "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/7514"
            }
        ]
    },
    {
        "id": 93432,
        "customFieldId": 10,
        "customField": {
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/10"
                }
            ]
        },
        "value": "Fulltime-Regular",
        "links": [
            {
                "rel": "canonical",
                "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/93432"
            }
        ]
    }
]

}

Answer №1

If you need to find a specific item in an array, consider using the Array.find() method:

var obj = {"items":[{"id":3854,"customFieldId":1,"customField":{"links":[{"rel":"canonical","href":"https://example.com/customFields/1"}]},"value":"Yes","links":[{"rel":"canonical","href":"https://example.com/customFieldValues/3854"}]},{"id":7514,"customFieldId":3,"customField":{"links":[{"rel":"canonical","href":"https://example.com/customFields/3"}]},"value":"No","links":[{"rel":"canonical","href":"https://example.com/customFieldValues/7514"}]},{"id":93432,"customFieldId":10,"customField":{"links":[{"rel":"canonical","href":"https://example.com/customFields/10"}]},"value":"Fulltime-Regular","links":[{"rel":"canonical","href":"https://example.com/customFieldValues/93432"}]}};

var customId = 3;
var result = obj.items.find((obj) => obj.customFieldId === customId);
console.log(result);

Answer №2

If you want to extract the fieldId from an object based on a specific customFieldId, you can use a simple loop like this:

for(let index = 0; index < myObj.items.length; index++) {
 let customFieldId = myObj.items[index].customFieldId;
 let id = myObj.items[index].id;

 if(customFieldId === 3 || customFieldId === 1) {
  console.log("The custom field ID is: " + customFieldId + " and the id is: " + id);
 }
}

Answer №3

One way to extract specific data from a JSON object is by using the filter function.

var data = {"items" :[{"id":3854,"customFieldId":1,"customField":{"links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/1"}]},"value":"Yes","links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/3854"}]},{"id":7514,"customFieldId":3,"customField":{"links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/3"}]},"value":"No","links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/7514"}]},{"id":93432,"customFieldId":10,"customField":{"links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/10"}]},"value":"Fulltime-Regular","links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/93432"}]}]};

function findCustomFieldById(data) {
   return data.customFieldId == 3;
}

var filteredData = data.items.filter(findCustomFieldById);
console.log(filteredData); 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In Python 3, when using the requests library, a response of

When I run curl command, I receive back a unique hash value: $ curl "http://127.0.0.1:8002/hash/" -X POST -d 'data={"key":"value"}' { "hash": "9724c1e20e6e3e4d7f57ed25f9d4efb006e508590d528c90da597f6a775c13e5" } However, when using Python requ ...

Encountering a memory issue when trying to parse a hefty JSON file using the Jackson library in an Android

Currently, I am facing a challenge with parsing a substantial JSON response from the server using the Jackson library. The size of the JSON file is approximately 7-8 MB. The error that I encountered while running the code snippet below: ObjectMapper mapp ...

How to refresh an array in Angular 4 after inserting a new element using splice method?

I have a Angular list displayed in a table, and I need to insert an element at a specific position. I have tried using the following code: array.splice(index, 0, element); While everything seems fine in the console with the element being added at the corr ...

modify the values of directive elements using a templateUrl

HTML Template: I have an element in the template file seats.tpl.html like this: <select id="guest_table"> <option tables="emptySeats" ng-repeat="table in emptySeats" value="{{table.tableId}}">{{table.tableNumber}}</option& ...

How to implement the Ionic ToastController without being confined to a Vue instance

I am currently facing a challenge while trying to utilize the ToastController of Ionic outside a vue instance. I have created a separate actions file which will be loaded within the vue instance, handling a request. Within this request, certain validations ...

I am looking to dynamically generate HTML elements using AngularJS based on data from a JSON file

Although there are existing answers to this question, I have a specific approach that I need help with. I've already made progress but could use some guidance. This is my Controller : angular.module('dynamicForm.home-ctrl',[]) .con ...

Issue with code failing to insert object into an array

I've been struggling to add a group of objects from a JSON file into an array. Despite trying to use the push method, the length of the array remains at 0. I'm puzzled by what could be going wrong. The JSON data is being parsed correctly as I&apo ...

Accessing dropdown selection in Javascript-Json: A Comprehensive Guide

My dropdown list is being populated dynamically using json. The selected option from the first dropdown determines the options in a secondary dropdown. I need to use the selection from the second dropdown to automatically fill out two more fields. For exa ...

ng filtering with a controller-defined scope

I am currently working on a webpage with AngularJS and I am looking to implement some filters on the site. Here is the HTML code I have: <div ng-repeat="data in datas | filter:{area:course} | filter:{subject:subFilter} | filter:{city:cityFilter}"> ...

Refresh the dataTable once all external data has been successfully fetched

I am struggling to find the correct method for reloading a datatable. Here is my current process: Retrieve data through ajax for specific columns Generate a table with the fetched data Initialize the datatable for the table Make a new ajax request using ...

Utilizing JavaScript within my WordPress site

I'm experiencing some issues with my JavaScript code in WordPress. I have been trying to use the following code on my page, but it doesn't seem to work properly. Can someone please guide me on how to integrate this code within my WordPress page? ...

Eliminating duplicate outcomes when using JSON_ARRAYAGG with GROUP BY in MySQL

I have a question: SELECT id, JSON_ARRAYAGG(url) AS urlLinks FROM links WHERE id=832781 GROUP BY id; The output of this query shows duplicate image URLs in the column urlLinks: ["index.html", "index.html", "index.html", "index.html", "index.html"] Is t ...

Encountering a CORS policy issue while attempting to retrieve data from an API

I have been attempting to retrieve data from the DHL API, however, I keep encountering issues due to CORS policy blocking the request. Even though I have configured the CORS policy on my backend server, the error persists. What could be the issue? Here ...

Dealing with information obtained through ajax requests

Trying to send data from modal using ajax. Below is the code snippet I am using, however, it seems that the first IF block is causing issues. If I comment it out, I can access the $_POST['id'] variable, but otherwise, it doesn't work. ...

Leveraging Arrays with AJAX Promises

I am currently working on making multiple AJAX calls using promises. I want to combine the two responses, analyze them collectively, and then generate a final response. Here is my current approach: var responseData = []; for (var i=0; i<letsSayTwo; i++ ...

Creating a live child component using React

My issue lies with the menu component, which is supposed to dynamically load elements in another container component. Unfortunately, the child elements are not being rendered. App.js import React, { Component } from 'react'; import './App. ...

The Angular array stays undefined when JSON data is being passed

I am facing an issue with my API that provides JSON data related to football matches. Even after passing this data to the frontend (angular), I am encountering a problem where the array remains undefined. JSON Data: "match_id":"194200", "country_id":"41" ...

Enable a click event within an iFrame by clicking on an element in the parent document

I am attempting to trigger the click event of an element inside an iFrame (specifically a standard Twitter follow button) when clicking on an element within my main webpage. Below is the code snippet I have been experimenting with, but unfortunately it do ...

How can I detect a click event on an SVG element using JavaScript or jQuery?

Currently, I am developing a web application that utilizes SVG. However, I have encountered an issue: I am struggling to add a click event to each element within the SVG using jQuery. The problem arises when attempting to trigger the event; it seems like t ...

HashId plugin for Angular

Currently, I'm attempting to integrate into my Angular project built on the latest version. After discovering this definition file: // Type definitions for Hashids.js 1.x // Project: https://github.com/ivanakimov/hashids.node.js // Definitions by: ...