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.
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.
Share Improve this question edited May 18, 2013 at 22:48 Gal Appelbaum asked May 18, 2013 at 22:23 Gal AppelbaumGal Appelbaum 1,9556 gold badges21 silver badges33 bronze badges 5- It's not at all clear what you're asking here. – Pointy Commented May 18, 2013 at 22:25
- you want to reverse the JSON.stringify? – Dory Zidon Commented May 18, 2013 at 22:28
- 1 What are you trying to do? – Vivin Paliath 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"}" ) – jabbink 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. – nnnnnn Commented May 18, 2013 at 22:41
3 Answers
Reset to default 3I'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 ma that should've been a colon in between "PWD"
and "zzz"
, and a dot that should've been a ma in between 11111
and "_updatedAt"
. There's no way that JSON.stringify()
would have produced the string that you showed with those mistakes.)
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.
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...