Getting data from a weird array - arrays

I am trying to get som data out of a Facebook array. It has allways worked earlier, but I have problems now for some reason.
$json=json_decode($string,true);
$from= $json['0']['data']['from']['name'];
The array output:
{ "data": [
{
"id": "XXX",
"from": {
"name": "Random name",
"id": "ID123321"
},
And a lot of different parameters and arrays bellow.
What is wrong with my $from variable?

It coud be that there is more than one element in data
Try using $from= $json['0']['data'][0]['from']['name'];

Related

How can I select a field and nested array from a JSON using JSONPath?

From the contacts, I'd like to select the values in fields: "Id" (47) and everything from the nested array [doNotContact]. I could use some help defining the JSONPath-filter I should be using to select the values: 47 and each value inside the nested array.
{
"total": "1",
"contacts": {
"47": {
"id": 47,
"isPublished": true,
"dateAdded": "2015-07-21T12:27:12-05:00",
"createdBy": 1,
"createdByUser": "Joe Smith",
"doNotContact": [{
"id": 2,
"reason": 2,
"comments": "",
"channel": "email",
"channelId": null
}]
}
}
}
I have tried paths like: $.contacts.*.['id','doNotContact'] however, this does not seem to work. I am using the website: https://goessner.net/articles/JsonPath/ normally this would help me solve the problem.
Not all implementations support the comma-delimited selectors, e.g. ['id','doNotContact']. See the JSON Path comparison project site (specifically this test) for information as to which implementations support the syntax.
Secondly, please see this answer about omitting the dot before a bracket syntax

Extract just one value from json array result in vb6

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")

Karate: simplest way to validate value of array json response should not be empty

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: "#? _ != ''" }

Query an array of users based on an array of users

Basically I'm having trouble understanding how I would figure this out.
I have a document in a mongodb collection, and that document has field called friends which is an array of usernames.
I want to query through each username in the array friends, and have an array of those user documents. I'm terrible at explaining maybe if I draw this out it'll make sense.
mongodb document:
{
"_id": {
"$oid": "59a20e65f94cb5e924af774e"
},
"name": "Nick",
"friends": ["Jones","Mark","Mike"]
}
Now with this friends array, I want to search the same collection for an object with the "name" Jones, Mark, and Mike. When I find that object, I want to put it into an array.
Basically I want it to return this, (for this example let's say Jones, Mark, and Mike only have one friend, and that friend is Nick.
[{
"_id": {
"$oid": "59a20e65f94cb5e924af774e"
},
"name": "Jones",
"friends": ["Nick"]
},
{
"_id": {
"$oid": "59a20e65f94cb5e924af774e"
},
"name": "Mark",
"friends": ["Nick"]
},
{
"_id": {
"$oid": "59a20e65f94cb5e924af774e"
},
"name": "Mike",
"friends": ["Nick"]
}]
^ an array of three objects, which are all the friends of Nick.
If you need any more explanation please let me know, I'm terrible at this type of stuff.
For the record, I'm using node, and basic mongodb (not mongoose).
I believe you are looking for $in operator.
// doc.friends = ["Jones","Mark","Mike"]
db.collection.find({ name: { $in: doc.friends }})

Mapping an array inside an array with a JSON Reader

My JSON looks as follows:
{
"records": [
{
"_id": "5106f97bdcb713b818d7f1f1",
"cn": "lsacco",
"favorites": [
{
"fullName": "Friend One",
"uid": "friend1"
},
{
"fullName": "Friend Two",
"uid": "friend2"
}
]
}
]
}
When I try to use records.favorites as the root for my JSON reader, I do not get any results populated to my model. Is there a way to do this without having to resort to using an association? Note that in my case, records will only have one element despite it showing an array.
records.favorites isn't valid because the property doesn't exist.
You want:
records[0].favorites
records has been declared as an array so records.favorites will point to nothing in the json data file.
using the index in records should solve the problem.

Resources