0

So I don't know why Im struggling with this. It seems like I should be able to use Lodash or something to do this I would think.

I have an object that looks like.

  { "fdjkafdajkfasfjkdaslj": {
      "-LMUD-2APhiSLkEtVbM": {
        "profileName": "bob",
            "profilePic": "www.profilepic.com",
            "score": 73,
            "teamName": "lzs"
        }
    }
  }

I will not know the first to keys in the structure. Example from above. "fdjkafdajkfasfjkdaslj" or "-LMUD-2APhiSLkEtVbM".

I really just need to push these key values into an array.

       "profileName": "bob",
        "profilePic": "www.profilepic.com",
        "score": 73,
        "teamName": "lzs"

What is the best way to accomplish this in javascript (im actually a React.js)? I was thinking Lodash maybe but haven't found anything yet? Most of my searches for retrieving nested Key/Value data in Json indicate you would need a path but I will not have the path in this scenario. I won't know the top 2 keys in this object. The only thing I have is this predicable object structure with 2 unknown keys before my key/value data.

3
  • Can there be more keys inside an object Commented May 19, 2019 at 16:56
  • your JSON seems invalid, are the keys nested? Commented May 19, 2019 at 17:38
  • I was missing a colon on that first key (typo). Besides that, it is valid Json I believe. Commented May 19, 2019 at 17:44

3 Answers 3

2

If there will be only one key in object as in question sample then you can use Object.keys()

const data = [];

const obj = { "fdjkafdajkfasfjkdaslj" {
      "-LMUD-2APhiSLkEtVbM": {
        "profileName": "bob",
            "profilePic": "www.profilepic.com",
            "score": 73,
            "teamName": "lzs"
        }
    }
  };
  
  const outerObj = obj[Object.keys(obj)[0]];
  
  const item = outerObj[Object.keys(outerObj)[0]];
  
  data.push(item);

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

3 Comments

This did work. Technically I just needed the const outerObj = obj[Object.keys(obj)[0]];. That got me the inner key\value {} I needed to push into my array. Also your code snippet fails. Maybe because the : is missing on the first key. I had a typo in my origin post. If you fix it I'll accept the answer. Thank you!
I think my answer is correct according to your question. You got the idea how to get it done, that's matters the most.
Your answer definitely got me a solution, I really appreciate!! I was thinking more about others that may come upon this post in the future. I'll accept the answer but if you add a colon to the "fdjkafdajkfasfjkdaslj": value your code will run. I left it out by accident in my origin post (a typo error on my part). Your code is fine, it just wont run without that colon i the json object. Thanks again!
1

I think you'll want to use Object.values.

const realData = Object.values( Object.values( weirdThing )[0] )[0]

yourArray.push(realData)

1 Comment

This is a better solution if IE support is not required.
0

For JSON string, it can be found during parsing :

var obj, json = '{"fdjkafdajkfasfjkdaslj":{"-LMUD-2APhiSLkEtVbM":{"profileName":"bob","profilePic":"www.profilepic.com","score":73,"teamName":"lzs"}}}'

JSON.parse(json, (k, v) => v.profileName ? obj = v : v)

console.log( obj )

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.