I am working with a Harp project, using Jade templates. I have my _data.json populated with content and I'm having trouble accessing it the way I thought I could. Given this JSON structure:
{
"mountain-residence": {
"slug": "mountain-residence",
"title": "Mountain Residence",
"lead": "A southeast facing home...",
"thumb": "exterior/entry",
"sections": [
{
"exterior": {
"slug": "exterior",
"title": "Exterior Photos",
"lead": "Embracing an entry...",
"thumb": "terrace",
"photos": [
{
"slug": "southeast",
"alt": "Mountain Room Overlook",
"caption": "Porch, deck and spa terrace"
},
{
"slug": "terrace",
"alt": "Southeast Terrace",
"caption": "Spa deck and landscape terrace"
},
{
"slug": "entry",
"alt": "Entry Courtyard",
"caption": "Entry court and pergola"
},
{
"slug": "porch",
"alt": "Entry Porch",
"caption": "Timber entry shelter"
}
]
}
},
{
"interior": {
"slug": "interior",
"title": "Interior Photos",
"lead": "The interior spaces...",
"thumb": "mountain-room2",
"photos": [
{
"slug": "mountain-room2",
"caption": "Entry opening to the Mountain Room"
},
{
"slug": "dining",
"caption": "Dining Room into Mountain Room"
},
{
"slug": "cellar1",
"caption": "Wine Cellar and Tasting Room"
},
{
"slug": "den",
"caption": "Den and Family Hearth"
}
]
}
},
{
"design-sketches": {
"slug": "design-sketches",
"title": "Design Sketches",
"lead": "A careful site...",
"thumb": "shower",
"photos": [
{
"slug": "schematic",
"caption": "Exterior Elevation Study"
},
{
"slug": "elevation",
"caption": "Elevation Color Studies"
},
{
"slug": "shower",
"caption": "Outdoor stone shower"
}
]
}
}
]
}
}
I cannot figure out how to access a named object within an array, like mountain-residence.sections.exterior.title ("Exterior Photos"). Trying mountain-residence.sections['exterior'].title doesn't work either. I can access the same property with mountain-residence.sections[0].title, and my JSON seems to be valid, but I need to call the object with its name... Is there a way to do this?
For example, "mountain-residence" populates a page, and my general idea was to have each object in "sections" populate its own sub-page, which is why I want to be able to call it up by name through my JSON.
As stated in the comments, sections is an array, so only the index notation will work. If you want to get it by the name of the section, it needs to be an object either natively, or else transformed into an object by preprocessing code you write.
Related
I'm attempting to use Google Sheets to make an audio library that would translate nicely into json (via CSV or some kind of live feed), but I haven't got clue on how to make sub columns that I'll later on use as tags (genre, lyrics theme etc).
Here's the correct json output I want to get from my spreadsheet:
{
"Songs": [
{
"Title": "Bed of Roses",
"Artist": "Bon Jovi",
"Metadata": {
"LyricsTheme": [
"Love",
"Romantic"
],
"Genre": [
"Hard rock",
"Ballad"
]
}
},
{
"Title": "Blue Monday",
"Artist": "New Order",
"Metadata": {
"LyricsTheme": [
"A",
"B",
"C"
],
"Genre": [
"Synthpop",
"Postpunk",
"New wave"
]
}
}
]
}
And here is what I've tried so far with Google Sheets (but that's nowhere near how I want it to be):
Any help would be greatly appreciated.
You'll need to have all of your field names in the first row, and you create the nesting with slashes like this:
This will give you:
[
{
"Title": "Bed of Roses",
"Artist": "Bon Jovi",
"Metadata": {
"LyricsTheme": "Love, Romantic",
"Genre": "Hard Rock, Ballad"
}
},
{
"Title": "Blue Monday",
"Artist": "New Order",
"Metadata": {
"LyricsTheme": "Dogs, Cats, Birds",
"Genre": "Synthpop, Postpunk, New wave"
}
}
]
Finally, in the utility that you use to convert the CSV to JSON, You need to find the output setting that gives you a Top Level Property Name, or something similar.
I used convertcsv.com and it is in the output section.
That will give you:
{
"Songs": [
{
"Title": "Bed of Roses",
"Artist": "Bon Jovi",
"Metadata": {
"LyricsTheme": "Love, Romantic",
"Genre": "Hard Rock, Ballad"
}
},
{
"Title": "Blue Monday",
"Artist": "New Order",
"Metadata": {
"LyricsTheme": "Dogs, Cats, Birds",
"Genre": "Synthpop, Postpunk, New wave"
}
}
]
}
The problem is: I have 2 cucumber test reports in JSON format
I need to remove redundant key-value pairs from those reports and compare them, but I can't understand how to remove the unnecessary data from those 2 jsons because of their structure after JSON.parse (array or hash with many nested arrays/hashes). Please advice if there are some gems or known solutions to do this
JSON structure is e.g. :
[
{
"uri": "features/home_screen.feature",
"id": "as-a-user-i-want-to-explore-home-screen",
"keyword": "Feature",
"name": "As a user I want to explore home screen",
"description": "",
"line": 2,
"tags": [
{
"name": "#home_screen",
"line": 1
}
],
"elements": [
{
"keyword": "Background",
"name": "",
"description": "",
"line": 3,
"type": "background",
"before": [
{
"match": {
"location": "features/step_definitions/support/hooks.rb:1"
},
"result": {
"status": "passed",
"duration": 505329000
}
}
],
"steps": [
{
"keyword": "Given ",
"name": "I click OK button in popup",
"line": 4,
"match": {
"location": "features/step_definitions/registration_steps.rb:91"
},
"result": {
"status": "passed",
"duration": 2329140000
}
},
{
"keyword": "And ",
"name": "I click Allow button in popup",
"line": 5,
"match": {
"location": "features/step_definitions/registration_steps.rb:96"
},
"result": {
"status": "passed",
"duration": 1861776000
}
}
]
},
Since you are asking for a gem, you might try iteraptor I have created exactly for this kind of tasks.
It allows iterating, mapping and reducing the deeply nested structures. For instance, to filter out all the keys called "name" on all levels, you might do:
input.iteraptor.reject(/name/)
The more detailed description might be found on the github page linked above.
I'm constructing my own HTTP requests for use with the QuickBooks Online v3 REST API, 'rolling my own' in the words of Keith Palmer - Consolibyt's response to this post.
I particular, I want to create, update, and delete transactions and names using JSON-formatted request bodies. The documentation is very sparse with JSON examples, so this is all I could find:
{
...
"domain":"QBO"
"sparse":{"true","false"}
...
}
The API gives XML examples for creating, updating, and deleting transactions, and I have been able to use those in the API explorer successfully. (For other reasons, though, I don't want to work with XML. I want to work in JSON.)
In trying to figure out how to do the same thing with JSON, I did a read of an existing JournalEntry (in the API Explorer), getting this response and planning to build the request body starting from here:
{
"JournalEntry": {
"Adjustment": false,
"domain": "QBO",
"sparse": false,
"Id": "1056",
"SyncToken": "2",
"MetaData": {
"CreateTime": "2014-02-07T13:21:51-08:00",
"LastUpdatedTime": "2014-02-07T13:29:53-08:00"
},
"DocNumber": "4",
"TxnDate": "2014-02-08",
"CurrencyRef": {
"value": "USD",
"name": "United States Dollar"
},
"Line": [
{
"Id": "0",
"Description": "test entry",
"Amount": 102.0,
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": {
"PostingType": "Credit",
"AccountRef": {
"value": "57",
"name": "Miscellaneous Income"
}
}
},
{
"Id": "1",
"Description": "test entry (UPDATED!)",
"Amount": 252.0,
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": {
"PostingType": "Debit",
"AccountRef": {
"value": "29",
"name": "6240 Miscellaneous"
}
}
},
{
"Id": "2",
"Description": "test entry",
"Amount": 150.0,
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": {
"PostingType": "Credit",
"AccountRef": {
"value": "82",
"name": "12100 Inventory Asset"
}
}
}
]
},
"time": "2014-02-07T14:21:16.181-08:00"
}
Here's the request body I'm using for an Update operation (on the same JournalEntry page of the API Explorer) -- note that I'm only changing the DocNumber (from 4 to 12345):
{
"Adjustment": false,
"domain": "QBO",
"sparse": false,
"Id": "1056",
"SyncToken": "2",
"MetaData": {
"CreateTime": "2014-02-07T13:21:51-08:00",
"LastUpdatedTime": "2014-02-07T13:29:53-08:00"
},
"DocNumber": "12345",
"TxnDate": "2014-02-08",
"CurrencyRef": {
"value": "USD",
"name": "United States Dollar"
},
"Line": [
{
"Id": "0",
"Description": "test entry",
"Amount": 102.0,
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": {
"PostingType": "Credit",
"AccountRef": {
"value": "57",
"name": "Miscellaneous Income"
}
}
},
{
"Id": "1",
"Description": "test entry (UPDATED!)",
"Amount": 252.0,
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": {
"PostingType": "Debit",
"AccountRef": {
"value": "29",
"name": "6240 Miscellaneous"
}
}
},
{
"Id": "2",
"Description": "test entry",
"Amount": 150.0,
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": {
"PostingType": "Credit",
"AccountRef": {
"value": "82",
"name": "12100 Inventory Asset"
}
}
}
]
}
Here's the Response Body I get back:
{"Fault":{"Error":[{"Message":"An application error has occurred while processing your request","Detail":"System Failure Error: null","code":"10000"}],"type":"SystemFault"},"time":"2014-02-07T14:36:18.888-08:00"}
I'm also trying a sparse update, as per these instructions, just to keep things simple. The request body:
{
"sparse": "true",
"Id": "1056",
"SyncToken": "2",
"DocNumber": "12345"
}
The response body I get back (in bright red):
{"Fault":{"Error":[{"Message":"An application error has occurred while processing your request","Detail":"System Failure Error: null","code":"10000"}],"type":"SystemFault"},"time":"2014-02-07T14:05:05.197-08:00"}
I have a feeling the problem here is something small. Can you point out what it is? Alternatively, if you have a working example of a JSON request body (for create, update, and delete operations of a transaction, ideally), that would be very helpful too (as hopefully I could use it as a template).
You need to add Accept : application/json to your header.
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..