2

Here is my Json-String:

{
    "batchcomplete":"",
    "query":{
        "pages":{
            "104352":{
                "pageid":104352,
                "ns":0,
                "title":"student"
            }
        }
    }
}

I want to access tha first number, in this example "103452", not the on after pageid, although they always should be the same. I tried the following until know, but don´t get why it wont work.

JSONName.query.pages;

it always returns me Object object.

3
  • There's no such thing as a JSON Object – JSON is always a string. Commented Jun 17, 2018 at 19:28
  • thanks for that explanation, i mixed that up... Commented Jun 17, 2018 at 19:30
  • You can use Object.entries, Object.keys or Object.values to iterate through an object Commented Jun 17, 2018 at 19:31

2 Answers 2

2

Assuming you have a JavaScript object you can get the keys of an object which would contain your string.

const obj = {
  "batchcomplete": "",
  "query": {
    "pages": {
      "104352": {
        "pageid": 104352,
        "ns": 0,
        "title": "student"
      }
    }
  }
}

// Get the keys for pages
const keys = Object.keys(obj.query.pages);

// Print out the first key
console.log(keys[0]);

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

Comments

0

Might be easier to find it during the parsing:

var n, j = '{"batchcomplete":"","query":{"pages":{"104352":{"pageid":104352,"ns":0,"title":"student"}}}}';

var result = JSON.parse(j, (key, value) => (n = n || +key, value));

console.log( n ); console.log( result );

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.