I an using JSON and NODEJS
i am receiving data from TCP and i was receiving the data like this
[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" },{"c]
or
[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" },{"some n letters]
like this it is truncating while receiving TCP data i want to remove that half received data or is there any way to receive the full buffer
I want the Output to be
[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" }]
A clumsy, but simple way would be to take the string/buffer and try to parse using JSON.parse(). Wrap it in a try/catch and only set the final value when it succeeds.
var badJson = '[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" },{"c]';
try {
var result = JSON.parse(badJson);
}
catch(e) {
console.log('bad json, not parsing yet');
}
Related
If I log the full data from an Angular client I get
object { type: "message", target: {_}, errorCode: undefiend,
errorMessage: undefined, data: "{\"data\":[\"124",\"611\"]}",
lastEventId: ""}
I want to grab the {\"data\":[\"124",\"611\"]} part to send it as json to a client. Using JSON.parse(data.data) though gives me
data: "{\"data\":[\"124",\"611\"]}", lastEventId: ""}
Is it possible to just grab the "{\"data\":[\"124",\"611\"]}" since otherwise the client has problems with the deserialization.
Let's say you have your initial string in myobject_string.
Then, you extract the JSON to a Javascript object with: const myobject = JSON.parse(myobject_string).
Then, the data you are looking for is in myobject.data.
Look here for more example code on JSON.parse.
i know there are multiple similar topics, however trying their solutions doesn't give me expected result.
Input json string
data:"{"message": "{\"type\":\"CONTROL\",\"command\":\"REQUEST_STATUS_ALL\"}"}"
object declaration/parse:
const msg: Message = <Message>JSON.parse(data.data);
output:
{message: "{"type":"CONTROL","command":"REQUEST_STATUS_ALL"}"}
-values are not properly assigned, but instead in a text form.
the same object looks like this if it's initialized manually(in TS):
MessageĀ {type: "CONTROL", status: undefined, command: "REQUEST_STATUS_ALL", body: undefined}
What is the correct way to parse that json string into the Message object?
Thank you!
It seems the value for message was improperly encoded as a string. Calling JSON.parse a second time on the message property will get the result you want, though you might want to fix the underlying cause of the improperly encoded data instead.
parseMessage(data: string) {
const msgTemp = JSON.parse(data);
msgTemp.message = JSON.parse(msgTemp.message);
return <Message>msgTemp;
}
const msg = parseMessage(data.data);
so i have this data which is outputted from stringify(doc)
{
"id": "8467fdae-c38c-4b6e-9492-807d7c9eb97e",
"message": "send to nodejs"
}
but im not sure how can i go ahead in getting a single value from it, e.g message. Any help would be appreciated as ive tried different methods and they seem to not be working
After you use stringify method the value you have is string.
So You can not get single property from it, except you convert it become object again and get property it. Like this:
object = {
"id": "8467fdae-c38c-4b6e-9492-807d7c9eb97e",
"message": "send to nodejs"
};
// JSON string is value which you get after use Stringify method
jsonString = JSON.stringify(object);
// Convert jsonString to object again and get message property
message = JSON.parse(jsonString).message
stringify() will convert json to string.
Before running stringify(), doc is already json and you can get message by doc.message.
after running stringify(), the result will be string, if you want to get json back, you can use JSON.parse()
I am trying to parse a single value from a REST web service that I am testing.
I understand how to make the call and I see the JSON response in the Output window.
let request = Alamofire.request(.GET, "http://IP:PORT/jsonTest", parameters: ["s": "Ping?"])
.responseJSON{(_,_,data,_) in
var json = JSON(data!)
println(json)
The Console Output shows me:
{"NewDataSet":[
{"Table1":[
{"Column-A":"FirstA",
"Column-B":"FirstB"
},
{"Column-A":"SecondA",
"Column-B":"SecondB"
},
{"Column-A":"ThirdA",
"Column-B":"ThirdB"
}
]}
]}
What I would like to do now, is to display only the first value from Column-A - which in this example would be "FirstA".
I've been trying to use a code like this, but so far I am not getting anywhere...
println(json[0][0]["Column-A"].stringValue)
Any pointers much appreciated!
json["NewDataSet"][0]["Table1"][0]["Column-A"].stringValue
This is what you want. This is because your json starts with a dictionary and is formatted as dictionary>array>dictionary>array>dictionary. Note that json dictionaries are noted by { : , : } while arrays are noted as [ , ].
I have a document on a mongodb on Heroku. Each object in the document has a system generated object id in the form of
"_id": {
"$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
}
When I make a query and get the response from the server, I stringify the response using JSON.stringify and I log the object on the server console. When I do this the following gets logged:
this is the response: [{"creator":"al","what[place]":"home","what[time [start]":"22:00","what[time][end]":"","what[details]":"","who[]":["joe","kay","mau"],"_id":"xxxxxxxxxxxxxxxxxxxxxxxx"}]
Right after the full object gets logged, I try to log the id to make sure I can access it... I want to then pass the id to a different object so that I can have a reference to the logged object.
I have this right now:
var stringyfied = JSON.stringify(res);
console.log("this is the response: " + stringyfied);
console.log("id: " + stringyfied._id);
but when the item is logged I get
id: undefined
instead of
id: "xxxxxxxxxxxxxxxxxxxxxxxx"
No matter how I try to access the _id property, I keep getting undefined even though it get printed with the console.log for the full object
I've tried:
stringyfied.id
stringyfied["_id"]
stringyfied["id"]
stringyfied._id.$oid
stringyfied.$oid
you need to use JSON.parse(), cause JSON.stringify() is to convert to string, parse is to get the object. stringifiedid is a string
What is being returned is an array with one object in it.
The way to access the _id is with stringyfied[0]._id. However, it would be cleaner to pull the object out of the array instead.
There are a few ways you can do that. If this query will only ever return one result and that's all you'll want, you can use the findOne method instead. If the query may return more than a single document, then you will want to loop through the returned results.
I also agree with #dariogriffo that you'll need to use JSON.parse() on the stringified JSON variable.