Hi Im new to JSON this may be a basic question, im having data as a JSON object and i want to print the "message" data alone. My sample JSON data, in this how can i access message?
{
"name": "Usingtagproject",
"fan_count": 0,
"category": "Product/Service",
"feed": {
"data": [
{
"created_time": "2017-05-02T18:24:27+0000",
"message": "hii",
},
{
"created_time": "2017-05-02T09:26:37+0000",
"message": "Hi Google",
},
{
"created_time": "2017-05-02T09:24:26+0000",
"message": "Hi Demoproject",
}
],
}
JSON is essentially a map (keys with values), that has a specific syntax.
It completely depends on what language you're accessing it in, but you can assume that most languages will have key indexing, so that you can say:
string name = my_json['name']
list<map> data = my_json['data']
for data_map in data:
print "found message: " + data_map['message']
Related
The result of json that I received from the SMS sending panel by Rest API is as follows and display in textbox :
{
"status": "OK",
"code": "OK",
"message": "Ok",
"data": {
"messages": [
{
"number": "+9710001529",
"message": "Hello World",
"sender": "+97911308600",
"time": "2022-07-12T20:12:14Z",
"type": "normal"
},
{
"number": "+9710001529",
"message": "Just For Test",
"sender": "+979051931024",
"time": "2022-06-28T23:15:22Z",
"type": "normal"
},
{
"number": "+9710001529",
"message": "Test",
"sender": "+979565547989",
"time": "2022-01-28T16:04:50Z",
"type": "mobilepanel"
},
{
"number": "+9710001529",
"message": "Comment",
"sender": "+979102900089",
"time": "2018-06-16T22:23:23Z",
"type": "normal"
}
]
},
"meta": {
"total": 37,
"pages": 4,
"limit": 10,
"page": 0,
"prev": null,
"next": "http://0.0.0.0:80/v1/inbox?limit=10\u0026page=1"
}
}
Now, I need to fetch the first mobile number with the name "sender" and show it in textbox for searching in database. The result should look like this: +97911308600.
I downloaded VB-JSON, VB6 JSON Parser Class Library and try to get a specific field from JSON data structure. if json result was not array like this code works good:
{
"status": "OK",
"code": "OK",
"message": "Ok",
"data": {
"credit": 2655946.6574392905
}
}
my code :
Dim p As Object
Set p = json.parse(Text1.text)
Debug.Print p.Item("data").Item("credit")
My expected output :
2655946.6574392905
The problem is when the Json result is a collection of arrays. How can I read first "sender" value as Mobile number just like value of "credit"?
Please guide me or post code. Thank you
Short answer:
o.item("data").item("messages").item(1).item("number")
Easy way to find out..
Put a breakpoint right after the .Parse(...) call and then stop your execution and proceed to the "Immediate Window".
Start debugging using the TypeName(...) method from VB6.
?TypeName(o) => Dictionary
?TypeName(o.item("data")) => Dictionary
?TypeName(o.item("data").item("messages")) => Collection
So, this is how the parser renders the array of objects. Instead of a Dictionary, it is a collection. Collections can be indexed via numeric keys (base 1).
Thus,
?TypeName(o.item("data").item("messages").item(1)) => Dictionary
Now, we're back to a Dictionary, so we're back on a JSON object as opposed to an array. You could also have done .Count on the Collection if you needed to iterate.
And, then, finally, extract the field from the collection you wanted:
o.item("data").item("messages").item(1).item("number") => +9710001529
And, you're there.
Try using tiny mdJson.bas module from this thread instead of some bloated components and libraries.
It's a single file JSON parser implementation that uses nested VBA.Collections to represent JSON objects and arrays and uses JSON Path expressions to access nested data.
Your code would become this
Dim p As Object
Set p = JsonParseObject(Text1.text)
Debug.Print JsonValue(p, "data/credit")
You can read the first sender like this
Debug.Print JsonValue(p, "messages/0/sender")
. . . or using JSON Path expressions like this
Debug.Print JsonValue(p, "$.messages[0].sender")
response= [
{
"id": "123",
"name: "user1",
"location": "USA"
},
{
"id": "133",
"name: "user2",
"location": "CANADA"
},
{
"id": "",
"name": "user3",
"location": "INDIA"
}
]
I am trying to validate the above json array using the schema and want to validate that values of my keys should not be empty or blank.
I am using the below code:
* def schema = {"id": "#notnull", "name": "#notnull", "location": "#notnull"}
* match each response contains schema
But even though my response has "id":"" -- id is empty/blank in the 3rd index in json array. Still I get a pass in my scenario.
Is this the correct way or is there any other way to handle empty values in schema for a json array response.
Your help would be appreciated.
Thank you
Ideally we should have #notblank but we don't because it is so rare.
Do this instead:
* match each response contains { id: "#? _ != ''" }
My Code here return the response like this
{
"code": 422,
"message": "The given data was invalid.",
"errors": {
"0": {
"first_name": [
"The first name field is required."
]
},
"1": {
"last_name": [
"The last name field is required."
]
},
"2": {
"mobile": [
"The mobile must be an integer.",
"The mobile must be at least 9."
]
}
}
}
I need to combine objects and remove numbers to return like this
{
"code": 422,
"message": "The given data was invalid.",
"errors": {
"first_name": [
"The first name field is required."
],
"last_name": [
"The last name field is required."
],
"mobile": [
"The mobile must be an integer.",
"The mobile must be at least 9."
]
}
}
private function transformErrors(ValidationException $exception)
{
$errors = [];
foreach ($exception->errors() as $field => $message) {
$errors[] = [
$field => $message
];
}
return (object)$errors;
}
what is best way to handle response in this way
If I understood correctly, you just want to transform multidimensionnal array into a simple array.
You can try to use :
private function transformErrors(ValidationException $exception)
{
return array_values($exception->errors());
}
You can use array_values() function to gather the values from your errors array. Something like this:
$yourArray['errors'] = array_values($yourArray['errors']);
I am trying to update a field in a document with an array. I want to add an array to the field "products". I tried this:
POST /index/type/1/_update
{
"doc" :{
"products": [
{
"name": "A",
"count": 1
},
{
"name": "B",
"count": 2
},
{
"name": "c",
"count": 3
}
]
}
}
this is the error response I am getting when I try and run the code:
{
"error": {
"root_cause": [
{
"type": "mapper_parsing_exception",
"reason": "failed to parse [products]"
}
],
"type": "mapper_parsing_exception",
"reason": "failed to parse [products]",
"caused_by": {
"type": "illegal_state_exception",
"reason": "Can't get text on a START_OBJECT at 1:2073"
}
},
"status": 400
}
Anyone know what I am doing wrong?
The message "Can't get text on a START_OBJECT" means that Elasticsearch was expecting an entry of type "string" but you are trying to give an object as input.
If you check Kibana you will find that the field "products" exists there and is defined as a string. But since you are entering a list of dictionaries then the field "products" should have been defined from the beginning as an object (preferably with dynamic fields in it). An example would be (see full example at https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic.html )
"products": {
"dynamic": true,
"properties": {}
}
However since you already have the index then you can't change the mapping so you would need to delete the index, do the mapping beforehand and then do the update.
{
"code": 2001,
"message": "todays usage",
"results": [
{
"date": "2015-03-20",
"download": 7.063141,
"mac": "18f46ab79f0d",
"upload": 16.086909
}
],
"status": 200
}
how to get value of download from this json object ?i want to get the value of download and upload.
Example: if the above json was assigned to a variable called jsonObject:
var downloadValue = jsonObject.results[0].download;
var uploadValue = jsonObject.results[0].upload;