Home > Enterprise >  json extract keys without knowing variable data
json extract keys without knowing variable data

Time:06-14

just started with json. I have something similar to this data (this data is called Data0012.json and I cant edit it or change the -162.65 to another thing ... also ... that first data, changes, so I cant call it):

{
    "-162.65": {
        "Player": "DntFuckwithMeMTFrs",
        "Hands": "44",
        "(BTN) PFR (2-2,25) (16 )": "13",
        "!!!0 All-In Equity Adjusted BB/100": "-162.65",
        "BH_MTT_3Bet (BB vs BU open)": "50"
    },
    "-162.27": {
        "Player": "paramasivum",
        "Hands": "40",
        "(BTN) PFR (2-2,25) (16 )": "9",
        "!!!0 All-In Equity Adjusted BB/100": "-162.27",
        "BH_MTT_3Bet (BB vs BU open)": "67"
    },
    "-157.32": {
        "Player": "Fairline69",
        "Hands": "49",
        "(BTN) PFR (2-2,25) (16 )": "25",
        "!!!0 All-In Equity Adjusted BB/100": "-157.32",
        "BH_MTT_3Bet (BB vs BU open)": "17"
    },
}

I need to extract the keys from this data, without knowing this data (as if I havent seen the json), and without changing the json file... it comes as it's shown above.

I have to do this with a js. The keys will change with new json's, and I need to repeat this process and extract the keys to use them in the next script. How do I do this correctly.

I've tried with this:

<script src="./Data0012.json">
  var s = require('./Data0012.json');
  var data = JSON.parse(s);
  for (var key in data) {
    var value = data[key];
  }
  console.log(key);
</script>

but the console gives me an error with data:

Uncaught SyntaxError: Unexpected token ':' (at Data0012.json:2:14) attribute

what can I do to solve this. Help please.(dont give me negative points) Edited; Input: Data0012.json output wanted: Data0012.json's key's

CodePudding user response:

How to load json file

Provide you two ways for reference

❶ Wrap it up to create a Javascript object

<script type="text/javascript" src="Data0012.json"></script>

❷ Read file path

function loadJSON(filePath, callback) {
    var xobj = new XMLHttpRequest();
    xobj.overrideMimeType("application/json");
    xobj.open('GET', filePath, true);
    xobj.onreadystatechange = function() {
        if (xobj.readyState == 4 && xobj.status == "200") {
            // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
            callback(xobj.responseText);
        }
    };
    xobj.send(null);
}

loadJSON("Data0012.json", function(text){
    const data = JSON.parse(text);
    console.log(data);
});

How to get the key of an object

Object.keys(obj)

How to get data objects in different scripts

If you have a server, you can write api to edit and modify your json file, and call api in different scripts to get json data, or simply use Window.localStorage to keep updated data

// Get object key
const keys = Object.keys(obj);

// Stored data
localStorage.setItem('keys', keys);

// Call method to get stored data when in different scripts
const keys = localStorage.getItem('keys');

CodePudding user response:

If you want to extract the keys of the JSON object you can try this

const data = {
    "-162.65": {
        "Player": "DntFuckwithMeMTFrs",
        "Hands": "44",
        "(BTN) PFR (2-2,25) (16 )": "13",
        "!!!0 All-In Equity Adjusted BB/100": "-162.65",
        "BH_MTT_3Bet (BB vs BU open)": "50"
    },
    "-162.27": {
        "Player": "paramasivum",
        "Hands": "40",
        "(BTN) PFR (2-2,25) (16 )": "9",
        "!!!0 All-In Equity Adjusted BB/100": "-162.27",
        "BH_MTT_3Bet (BB vs BU open)": "67"
    },
    "-157.32": {
        "Player": "Fairline69",
        "Hands": "49",
        "(BTN) PFR (2-2,25) (16 )": "25",
        "!!!0 All-In Equity Adjusted BB/100": "-157.32",
        "BH_MTT_3Bet (BB vs BU open)": "17"
    },
}

const keys = Object.keys(data) // ["-162.65","-162.27","-157.32"]

  • Related