skip to Main Content

can anyone help me parse through a json
i want to read through this json

"GB": {
        "0005000010105A00": {
            "title_id": "0005000010105A00",
            "eshop_id": "20010000000023",
            "product_code": "WUP-N-HNNA",
            "name": "Netflix",
            "platform": 124,
            "platform_device": "WUP",
            "publisher": 584,
            "banner_url": "https://kanzashi-wup.cdn.nintendo.net/i/51fdaa8b1ebb0ea9119b3ccd6456e641c45f5a2d9065f29b5c48f9e2b4b7127e.jpg",
            "icon_url": "https://kanzashi-wup.cdn.nintendo.net/i/131a440b27b50e257cfdad2dc79b4ff4cb8d9b8a2b683efb0884a608430841f6.jpg",
            "data_size": "63963276",
            "description": "Instantly watch movies and TV episodes streaming from Netflix right to your TV via your Wii U console. Watch as often as you want, anytime you want. Download and install Netflix to get started. New Netflix members can start their one-month free trial today. Cancel anytime. Netflix membership required. Service only available in certain countries. Visit netflix.com for details. Subscription required.",
            "availability": {
                "eshop": true,
                "retail": false,
                "dates": {
                    "eshop": "2012-11-29",
                    "retail": null
                }
            },
            "screenshots": [
                "https://kanzashi-wup.cdn.nintendo.net/i/638462c1705a0218f4f51b2f89c087ca2ad991554124a8c286d6ba42dc4e2d7d.jpg",
                "https://kanzashi-wup.cdn.nintendo.net/i/93823895f675328ab5018ed6909532eb5d7e267b8ff7d7d2ef743c35dd540475.jpg",
                "https://kanzashi-wup.cdn.nintendo.net/i/fd193363aeb094f839ca58c23753b41a52e11d3c8e49652eff61632218c9bb6d.jpg"
            ],
            "movies": []
        },

And just get the ID
is there anyway to look for the name and get the ID back? (the id is the 0005000010105A00)
thanks!

2

Answers


  1. I’m going to make a few assumptions to try to help you.

    First, I’m guessing you have an object like this:

    {
      "GB": {
        ...
      }
    }
    

    where each entry in the "GB" object is an object with the form you described.

    If so, here’s how I would select the object you provided and return it’s "id" (typically called a "key" in the JavaScript community).

    // Get your json data from somewhere like a file or URL and parse it into an object.
    let myObject = JSON.parse(myJsonData);
    
    // Create our id variable.  If we don't find the desired name, id will be undefined.
    function getId (name) {
      let id;
      Object.entries(myObject.GB).forEach(([key,obj]) => {
        if (obj.name === name) {
          id = key;
        }
      });
      return id;
    }
    
    // now netflixId will be the desired id, or undefined if there's no such name found.
    let netflixId = getId('Netflix');
    

    I hope that helps.

    For future reference, you’ll do better posting what you’ve tried already.

    Login or Signup to reply.
  2. If your JSON is in a .json file, you can access it in code with require:

    const filename = 'your-file-name'
    const data = require(filename)
    

    Then use Sir Robert’s answer to get the ID, or install and then use the very popular lodash library’s get function:

    const _ = require('lodash')
    
    const filename = 'your-file-name'
    const data = require(filename)
    
    const netflixId = _.get(data, 'ID')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search