Hello i have the following JSON:
[
{
"name": "Donation",
"collection": {
"name": "Donation Company 1",
"collection": {
"id": 1,
"category": "Donation",
"name": "Some Donation",
"price": 10,
"description": "Hahahaha",
"company": "Donation Company 1"
}
}
},
{
"name": "Donation",
"collection": {
"name": "Donation Company 1",
"collection": {
"id": 2,
"category": "Donation",
"name": "Another Donation",
"price": 50,
"description": "LoL",
"company": "Donation Company 1"
}
}
},
{
"name": "Insurance Company 1",
"collection": {
"name": "Hehe",
"collection": {
"id": 3,
"category": "Insurance",
"name": "Lorem Ipsum Solor",
"price": 25,
"description": "Lmao",
"company": "Insurance Company 1"
}
}
},
{
"name": "Insurance Company 2",
"collection": {
"name": "Donation Company 1",
"collection": {
"id": 5,
"category": "Insurance",
"name": "Sample Extra",
"price": 500,
"description": "Lorem ipsum dolor",
"company": "Insurance Company 2"
}
}
}
]
I'm having trouble trying to group the similar items together, as in all items in the same category appear in the same category array.
And in each category all items that have the same company end up in the same company array.
This is an example what i am trying to accomplish, i used an online editor to construct the JSON:
expected-json-output-when-saved
I used the laravel collect() helper function to construct my array of objects, i have tried several combinations in foreach loops and have been at it for hours but have still not been able to get the expected JSON returned.
Assuming you json_decode() that JSON string and stored it in a $json variable, you can do the following:
$collection = collect($json);
$collection = $collection->groupBy('collection.collection.category');
Dump the $collection variable to see the result.
Related
I have this bit of json:
[{
"id": 123,
"name": "Ice",
"categories": [{
"products": [{
"id": "66642",
"display_name": "Ice A"
},
{
"id": "62466",
"display_name": "Ice B"
},
{
"id": "62466",
"display_name": "Ice B duplicate"
}
]
}]
},
{
"id": 97,
"name": "Cakes",
"categories": [{
"products": [{
"id": "14518",
"display_name": "Cake 1"
},
{
"id": "14105",
"display_name": "Cake 2"
}
]
}]
}
]
I'm using jq 1.6 and I'm trying to get this output:
[{
"id": "62466",
"name": "Ice B",
"category": "Ice"
},
{
"id": "66642",
"name": "Ice A",
"category": "Ice"
},
{
"id": "14105",
"name": "Cake 2",
"category": "Cakes"
},
{
"id": "14518",
"name": "Cake 1",
"category": "Cakes"
}]
I've tried with this code:
.[].name as $x | map(.categories[].products[]) | unique_by(.id) | .[] |
[{
id: .id,
name: .display_name,
category: $x
}]
But I get this output:
[
{
"id": "14105",
"name": "Cake 2",
"category": "Ice"
}
]
[
{
"id": "14518",
"name": "Cake 1",
"category": "Ice"
}
]
[
{
"id": "62466",
"name": "Ice B",
"category": "Ice"
}
]
[
{
"id": "66642",
"name": "Ice A",
"category": "Ice"
}
]
[
{
"id": "14105",
"name": "Cake 2",
"category": "Cakes"
}
]
[
{
"id": "14518",
"name": "Cake 1",
"category": "Cakes"
}
]
[
{
"id": "62466",
"name": "Ice B",
"category": "Cakes"
}
]
[
{
"id": "66642",
"name": "Ice A",
"category": "Cakes"
}
]
I looks like as there are two names
Ice
Cakes
It's multiplying the output by two , one for each name instead of just returning the 4 products and I can't get the result into and array of objects. Here is a reproducible code in jqplay. Because there are some duplicate records, ie: products that have more than one category I need them to be unique. How can i fix this?
Assigning the category name in a variable and then transforming each product with unique id:
map(
.name as $category
| .categories[].products
| unique_by(.id)
| .[]
| { id, name: .display_name, $category }
)
This map produces the 4 unique items.
jq 'map(
(.categories[].products | unique_by(.id)[] | {id, name: .display_name})
+ {category: .name}
)'
[
{
"id": "62466",
"name": "Ice B",
"category": "Ice"
},
{
"id": "66642",
"name": "Ice A",
"category": "Ice"
},
{
"id": "14105",
"name": "Cake 2",
"category": "Cakes"
},
{
"id": "14518",
"name": "Cake 1",
"category": "Cakes"
}
]
Demo
This question already has answers here:
How to deserialize JSON with duplicate property names in the same object
(3 answers)
Closed 6 years ago.
I'm trying to parse a JSON string to a JObject, but somehow it only parses the first object of an Array.
This is a part of a JSON string
{
"Categories": [
{
"Category": [
{
"ID": "1",
"Description": "Kochen/Backen",
"IsActive": "True"
}
],
"Category":[
{
"ID": "2",
"Description": "Sport",
"IsActive": "True"
}
],
"Category": [
{
"ID": "3",
"Description": "Begleitung 2",
"IsActive": "True"
}
]
}
],
And after JObject.Parse, I can see it loaded:
{
"Categories": [
{
"Category": [
{
"ID": "3",
"Description": "Begleitung 2",
"IsActive": "True"
}
]
}
],
So why are the first 2 Categories not parsed?
I'm not a pro with JSON, so I don't know if the string is correct that way.
Thanks for your help
Your JObject cannot hold duplicate keys. The dictionary in the parent list, has multiple entries with same key Category:
{
"Category": [
{
"ID": "1",
"Description": "Kochen/Backen",
"IsActive": "True"
}
],
"Category":[
{
"ID": "2",
"Description": "Sport",
"IsActive": "True"
}
],
"Category": [
{
"ID": "3",
"Description": "Begleitung 2",
"IsActive": "True"
}
]
}
So the other keys are overwritten after parsing and the last item with id 3 becomes the final value. Consider restructuring the keys into say Category1, Category2 and Category3
OK, this is probably a REAL simple request.
But, I have 4 JSON files, well, I get JSON when doing a GET to a
//some IP address/some directory/some sub directory/name of folder
So I get back let's just say, NETWORKS, SITES, RESOURCES, COMPONENTS
Each one below NETWORKS is the CHILD...
Ok, next: I need to COMBINE these JSON files to populate JSTree (from jstree.com)
I have a basic JSON file like this and it WORKS beautifully:
(NOTE: the ID's below are irrelevant and DO NOT match in the REAL examples farther down here.)
The intent here is to JOIN all four JSON objects which I'm getting through a RESTful environment to JAVA API's that GET the data from the database.
[
{
"data": "Network 1",
"metadata": {"id" : "n1"},
"children": [ {
"data": "Site 1",
"metadata": {"id" : "s1"},
"children": [ {
"data": "Resource 1",
"metadata": {"id" : "r1"},
"children": [
{
"data": "Component 1",
"metadata": {"id" : "c1"}
},
{
"data": "Component 2",
"metadata": {"id" : "c2"}
},
{
"data": "Component 3",
"metadata": {"id" : "c3"}
} ] } ] },
"Site 2",
"Site 3",
"Site 4"]
}
]
Here's my dilemma:
I need to combine the following JSON files: SITES, RESOURCES, and COMPONENTS
Simple right? Not so much.
Here's a sample of each of the lower level JSON Objects:
NETWORKS:
[
{
"id": "23ef0d23-0d8d-4466-98da-81ef30791773",
"notes": "This is a network for network 1",
"name": "n1"
},
{
"id": "b4b46748-511a-49bf-9d22-8da014c76cc2",
"notes": "This is a network for network 2",
"name": "n2"
},
{
"id": "678b4a01-a6a6-449f-966d-c50c74964729",
"notes": "This is a network for network 3",
"name": "n3"
},
{
"id": "8e2822b1-49a8-498e-979b-2849cfa82148",
"notes": "This is a network for network 4",
"name": "n4"
}
]
SITES:
[
{
"id": "05683e7b-e471-4417-bead-317cfcbfaf30",
"name": "s1",
"networkId": "23ef0d23-0d8d-4466-98da-81ef30791773",
"notes": "This is site 1"
},
{
"id": "de8d654c-f9c4-4a4e-8742-32794b218b54",
"name": "s2",
"networkId": "23ef0d23-0d8d-4466-98da-81ef30791773",
"notes": "This is site 2"
},
{
"id": "16b2b1cf-2991-4717-ae65-2158700fa95d",
"name": "s3",
"networkId": "8e2822b1-49a8-498e-979b-2849cfa82148",
"notes": "This is site 3"
}
]
RESOURCES:
[
{
"id": "26db6a18-5099-4117-9adb-b8c808a3c478",
"networkId": "23ef0d23-0d8d-4466-98da-81ef30791773",
"siteId": "05683e7b-e471-4417-bead-317cfcbfaf30",
"name": "r1",
"notes": "This is Resource 1"
},
{
"id": "26ad2b53-f4b2-41c1-a618-d9e710452b7f",
"networkId": "23ef0d23-0d8d-4466-98da-81ef30791773",
"siteId": "05683e7b-e471-4417-bead-317cfcbfaf30",
"name": "r2",
"notes": "This is Resource 2"
}
]
And Finally, COMPONENTS:
[
{
"id": "6e8d13ad-9eb6-4213-bf84-a6e91d2e1460",
"resourceId": "26ad2b53-f4b2-41c1-a618-d9e710452b7f",
"siteId": "05683e7b-e471-4417-bead-317cfcbfaf30",
"networkId": "23ef0d23-0d8d-4466-98da-81ef30791773",
"name": "Component 1",
"notes": "This is my very first component - Yay!"
},
{
"id": "8f18cca3-378e-4f9b-8a39-eb2285fa61fd",
"resourceId": "26ad2b53-f4b2-41c1-a618-d9e710452b7f",
"siteId": "05683e7b-e471-4417-bead-317cfcbfaf30",
"networkId": "23ef0d23-0d8d-4466-98da-81ef30791773",
"name": "Component 2",
"notes": "This is my Second component - Yay!"
},
{
"id": "539370a6-577f-477d-a6ea-d45efd7e65aa",
"resourceId": "26ad2b53-f4b2-41c1-a618-d9e710452b7f",
"siteId": "05683e7b-e471-4417-bead-317cfcbfaf30",
"networkId": "23ef0d23-0d8d-4466-98da-81ef30791773",
"name": "Component 3",
"notes": "This is my Third component - Yay!"
}
]
Ultimately, when combined, it's GOT to look like the very first JSON example which WORKS for JSTree.
Thank you all for helping.
Regards
Here's my JSON
{
"userReviewList": [{
"userReviewId": "789021517",
"body": "My shopping experience has bla bla bla",
"date": "Apr 16, 2013",
"name": "Djdannybhhhhh",
"rating": 5,
"title": "Awesome!",
"voteCount": 0,
"voteSum": 0,
"viewUsersUserReviewsUrl": "https://xxxxxx.com/us/rexxxws?usxxxxileId=xxxxxx",
"voteUrl": "https://xxxxxx.com/us/rexxxws?usxxxxileId=xxxxxx",
"reportConcernUrl": "https://xxxxxx.com/us/rexxxws?usxxxxileId=xxxxxx",
"reportConcernExplanation": "xxxxxxxxxxxxxxxxxxxxxxxxxx.",
"customerType": "Customers",
"reportConcernReasons": [{
"reasonId": "0",
"name": "Choose One"
}, {
"reasonId": "1",
"name": "This review contains offensive material"
}, {
"reasonId": "8",
"name": "This review is not a review or is off-topic"
}, {
"reasonId": "9",
"name": "I disagree with this review."
}, {
"reasonId": "7",
"name": "My concern isn't listed here."
}
]
}, {
"userReviewId": "780537396",
"body": "xxxxxxxxxxxxxxxxxxxxxx",
"date": "Apr 2, 2013",
"name": "Majak6",
"rating": 5,
"title": "Smart!",
"voteCount": 0,
"voteSum": 0,
"viewUsersUserReviewsUrl": "https://xxxxxx.com/us/rexxxws?usxxxxileId=xxxxxx",
"voteUrl": "https://xxxxxx.com/us/rexxxws?usxxxxileId=xxxxxx",
"reportConcernUrl": "https://xxxxxx.com/us/rexxxws?usxxxxileId=xxxxxx",
"reportConcernExplanation": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
"customerType": "Customers",
"reportConcernReasons": [{
"reasonId": "0",
"name": "Choose One"
}, {
"reasonId": "1",
"name": "This review contains offensive material"
}, {
"reasonId": "8",
"name": "This review is not a review or is off-topic"
}, {
"reasonId": "9",
"name": "I disagree with this review."
}, {
"reasonId": "7",
"name": "My concern isn't listed here."
}
]
}
]
}
I don't want to create separate class using json2csharp as I have already one class with specific properties I want to deserialize json to that class. That class has 'body', 'date', 'title', 'votecount' property (which are in json) & also some other properties are also in there, which are not in JSON. How can I do that in my Windows Store App using Newtonsoft Json library.
I guess this is a big ask. Using PHP I grab this Facebook Graph API json feed json feed link
{
"data": [
{
"id": "10369058551_10150645263758552",
"from": {
"name": "Land Rover",
"category": "Cars",
"id": "10369058551"
},
"message": "This week's Land Rover photo of the week is by Rodrigo Beja. Don't forget to submit your best photos each week to get featured.",
"link": "http://www.facebook.com/photo.php?fbid=10150645263678552&set=a.443106273551.221975.10369058551&type=1",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yz/r/StEh3RhPvjk.gif",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/10369058551/posts/10150645263758552"
},
{
"name": "Like",
"link": "http://www.facebook.com/10369058551/posts/10150645263758552"
}
],
"privacy": {
"description": "United Kingdom",
"value": "CUSTOM"
},
"type": "photo",
"object_id": "10150645263678552",
"created_time": "2012-02-03T17:19:03+0000",
"updated_time": "2012-02-03T22:32:28+0000",
"likes": {
"data": [
{
"name": "Mandy Elder",
"id": "100000250758731"
}
],
"count": 85
},
"comments": {
"data": [
{
"id": "10369058551_10150645263758552_6835532",
"from": {
"name": "John Sharp",
"id": "652940638"
},
"message": "This photo was used as part of the g4 challenge publicity photo pack around 2005-6 \nI remember because at the time I used it as a profile pic on msn \n\nAwesome photo - sums it up perfectly - I love it!!",
"created_time": "2012-02-03T21:39:48+0000"
},
{
"id": "10369058551_10150645263758552_6835852",
"from": {
"name": "Kathryn Piddington",
"id": "777370532"
},
"message": "Mud is good for the soul :)",
"created_time": "2012-02-03T22:32:28+0000"
}
],
"count": 14
}
},
{
"id": "10369058551_355476307803889",
"from": {
"name": "Land Rover",
"category": "Cars",
"id": "10369058551"
},
"message": "Click below to watch the new Range Rover Sport advert and catch up on the latest Land Rover and Range Rover news in this week\u2019s round-up. ",
"picture": "http://external.ak.fbcdn.net/safe_image.php?d=AQAx7xz602rfhyIn&w=90&h=90&url=http\u00253A\u00252F\u00252Fblog.landrover.com\u00252Fwp-content\u00252Fuploads\u00252F12my_rrs_044_LowRes.jpg",
"link": "http://blog.landrover.com/vehicles/the-land-rover-and-range-rover-weekly-19-3578.html#axzz1lKthI4F1",
"name": "The Land Rover and Range Rover Weekly 19 | Land Rover Blog",
"caption": "blog.landrover.com",
"description": "In this week\u2019s round-up of all things Land Rover and Range Rover, the new Range Rover Sport advert, Which? figures reveal the running costs of the Range Rover Evoque in comparison with competitors and a history of Land Rover narrated by Ranulph Fiennes.",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yD/r/aS8ecmYRys0.gif",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/10369058551/posts/355476307803889"
},
{
"name": "Like",
"link": "http://www.facebook.com/10369058551/posts/355476307803889"
}
],
"privacy": {
"description": "United Kingdom",
"value": "CUSTOM"
},
"type": "link",
"created_time": "2012-02-03T16:44:42+0000",
"updated_time": "2012-02-03T16:44:42+0000",
"likes": {
"data": [
{
"name": "Steve Nesbitt",
"id": "533982936"
}
],
"count": 10
},
"comments": {
"count": 0
}
},
{
"id": "10369058551_10150642499593552",
I'd like to the top level items to a mysql table, then each sub level to anaother table keeping a key / link between each table and then any sub level of a sublevel and linking this too. Is this possible?
I have got as far as this:-
$page = file_get_contents($url);
$json_output = json_decode($page, true);
I can't figure out how to loot hrough each row and add teh data to a Mysql table.
Thanks for your help in advance
Jonathan
Try to loop it as array then make sql query. Each array you put a unique key (for top level item). So, you can use your top level key to sub level foreign key..