0

I am trying to figure out if I JSON.Stringify an object like this:

{"m_id":"xxx","record": {"USER":"yyy","PWD","zzz","_createdAt": 11111."_updatedAt":00000},"state":"valid"} 

and then try to JSON.Parse out only the USER and PWD, not have to just call the object, but go through stringify. how would that work?

thanks.

5
  • It's not at all clear what you're asking here. Commented May 18, 2013 at 22:25
  • you want to reverse the JSON.stringify? Commented May 18, 2013 at 22:28
  • 1
    What are you trying to do? Commented May 18, 2013 at 22:29
  • What you say is not true, integers remain integers in a JSON but again, look at the answers below since I think you're trying to do something in an entirely wrong way. (About the integers: a = {a:123,b:'123'}; Object {a: 123, b: "123"} JSON.stringify(a) "{"a":123,"b":"123"}" ) Commented May 18, 2013 at 22:40
  • 1
    As I mentioned in my answer, there's no way that JSON.stringify() would have produced the string that you showed - that's not valid JSON. @jabbink - I think the OP meant that the property names get quoted when stringified. Commented May 18, 2013 at 22:41

3 Answers 3

3

I'm not sure why you're talking about stringifying your object. You'd stringify it if you needed to send the data across a network or something, not when you need to manipulate it in JS.

...how do I extract the strings in {...USER: "aaa", PWD: "zzz"...}?

Assuming you have a variable referring to the object, something like the following (with or without nice line breaks and indenting to make it readable, and with or without quotes around the property names):

var obj = { "m_id": "xxx", "record": { "USER": "yyy", "PWD" : "zzz", "_createdAt": 11111, "_updatedAt": 00000 }, "state": "valid" }; 

Then you can access the properties in the nested record object as follows:

console.log( obj.record.USER ); // outputs "yyy" console.log( obj.record.PWD ); // outputs "zzz" // etc. 

(Note: in your question you had two typos, a comma that should've been a colon in between "PWD" and "zzz", and a dot that should've been a comma in between 11111 and "_updatedAt". There's no way that JSON.stringify() would have produced the string that you showed with those mistakes.)

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

2 Comments

I guess you're right in that sense, but I was trying to avoid using that.. edited the question above.
If your requirement is to extract the "yyy" and "zzz" values then the way I've shown is the correct and simple way to do it. I don't understand why you want to "go through stringify" as per your updated question wording, that doesn't make sense. With an object you can access the properties directly. With a stringified representation you have to write code to parse the string. You can't use JSON.parse() to get out individual pieces.
0

If you want the strings "USER", "PWD" etc as an array, then use Object.keys.

If you want to iterate them, just use a normal for-in enumeration.

Comments

0

I might have misunderstood the question, but if I think it is what it is then try using

var tmp = JSON.parse(string_to_convert) 

this should suffice to convert your string to a proper Javascript Object

Then you can do

for(var index in tmp){ console.log(tmp[index]); } 

and this should list all the keys on the first set of properties. If you want to do a nested thing, then use recursion on the properties. Hope this makes sense...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.