How to match JSON-LD markup with REGEX? - html

I’m attempting to match an entire json-ld entry, regardless of specific markup, line breaks, etc.
Why doesn’t something as simple as this work:
\<script type\=\"application\/ld\+json\"\>(.*?)\<\/script\>
<script type="application/ld+json">
{
"#context": "http://schema.org",
"#type": "Recipe",
"author": "John Smith",
"cookTime": "PT1H",
"datePublished": "2009-05-08",
"description": "This classic banana bread recipe comes from my mom -- the walnuts add a nice texture and flavor to the banana bread.",
"image": "bananabread.jpg",
"recipeIngredient": [
"3 or 4 ripe bananas, smashed",
"1 egg",
"3/4 cup of sugar"
],
"interactionStatistic": {
"#type": "InteractionCounter",
"interactionType": "http://schema.org/Comment",
"userInteractionCount": "140"
},
"name": "Mom's World Famous Banana Bread",
"nutrition": {
"#type": "NutritionInformation",
"calories": "240 calories",
"fatContent": "9 grams fat"
},
"prepTime": "PT15M",
"recipeInstructions": "Preheat the oven to 350 degrees. Mix in the ingredients in a bowl. Add the flour last. Pour the mixture into a loaf pan and bake for one hour.",
"recipeYield": "1 loaf",
"suitableForDiet": "http://schema.org/LowFatDiet"
}
</script>
I expect the output to be everything inside the tag.

Here, we might want to bound our expression with an open json/ld tag as a start boundary, then collect all chars and newlines, and finally add a right boundary with closing script tag, maybe similar to:
(<script type="application\/ld\+json">)([\s\S]*)(<\/script>)
or
^(<script type="application\/ld\+json">)([\w\W]*)(<\/script>)$
DEMO
However, maybe here it would not be the best idea to user regular expressions and there should be so many methods that would do so much easier.
Test
const regex = /^(<script type="application\/ld\+json">)([\w\W]*)(<\/script>)$/gm;
const str = `<script type="application/ld+json">
{
"#context": "http://schema.org",
"#type": "Recipe",
"author": "John Smith",
"cookTime": "PT1H",
"datePublished": "2009-05-08",
"description": "This classic banana bread recipe comes from my mom -- the walnuts add a nice texture and flavor to the banana bread.",
"image": "bananabread.jpg",
"recipeIngredient": [
"3 or 4 ripe bananas, smashed",
"1 egg",
"3/4 cup of sugar"
],
"interactionStatistic": {
"#type": "InteractionCounter",
"interactionType": "http://schema.org/Comment",
"userInteractionCount": "140"
},
"name": "Mom's World Famous Banana Bread",
"nutrition": {
"#type": "NutritionInformation",
"calories": "240 calories",
"fatContent": "9 grams fat"
},
"prepTime": "PT15M",
"recipeInstructions": "Preheat the oven to 350 degrees. Mix in the ingredients in a bowl. Add the flour last. Pour the mixture into a loaf pan and bake for one hour.",
"recipeYield": "1 loaf",
"suitableForDiet": "http://schema.org/LowFatDiet"
}
</script>`;
const subst = `$2`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
DEMO
RegEx
If this expression wasn't desired, it can be modified or changed in regex101.com.
RegEx Circuit
jex.im visualizes regular expressions:

Related

json.load loads a string instead of json

I have a list of dictionaries written to a data.txt file. I was expecting to be able to read the list of dictionaries in a normal way when I load, but instead, I seem to load up a string.
For example - when I print(data[0]), I was expecting the first dictionary in the list, but instead, I got "[" instead.
Below attached is my codes and txt file:
read_json.py
import json
with open('./data.txt', 'r') as json_file:
data = json.load(json_file)
print(data[0])
data.txt
"[
{
"name": "Disney's Mulan (Mandarin) PG13 *",
"cast": [
"Jet Li",
"Donnie Yen",
"Yifei Liu"
],
"genre": [
"Action",
"Adventure",
"Drama"
],
"language": "Mandarin with no subtitles",
"rating": "PG13 - Some Violence",
"runtime": "115",
"open_date": "18 Sep 2020",
"description": "\u201cMulan\u201d is the epic adventure of a fearless young woman who masquerades as a man in order to fight Northern Invaders attacking China. The eldest daughter of an honored warrior, Hua Mulan is spirited, determined and quick on her feet. When the Emperor issues a decree that one man per family must serve in the Imperial Army, she steps in to take the place of her ailing father as Hua Jun, becoming one of China\u2019s greatest warriors ever."
},
{
"name": "The New Mutants M18",
"cast": [
"Maisie Williams",
"Henry Zaga",
"Anya Taylor-Joy",
"Charlie Heaton",
"Alice Braga",
"Blu Hunt"
],
"genre": [
"Action",
"Sci-Fi"
],
"language": "English",
"rating": "M18 - Some Mature Content",
"runtime": "94",
"open_date": "27 Aug 2020",
"description": "Five young mutants, just discovering their abilities while held in a secret facility against their will, fight to escape their past sins and save themselves."
}
]"
The above list is formatted properly for easy reading but the actual file is a single line and the different lines are denoted with "\n". Thanks for any help.
remove double quote in data.txt is useful for me。
eg. modify
"[{...},{...}]"
to
[{...},{...}]
Hope it helps!

WIX.com Schema JSON-LD Snippets Problems

WIX.com Schema Snippets Problems
I'm trying to install some schema snippets into my Wix website and I keep getting errors.
Somebody said
Your code has 4 tags in it, and you're using different Schema
types
Wix help says:
You can use one main JSON-LD, nesting is allowed.
I removed the script tags but I'm not sure how to "nest" the different Schema types. Any help would be greatly appreciated
-Jeff
The code is here: https://docs.google.com/document/d/1QWAkjjExws7IdYH25HuBbGN5gkeSDlusXlYdS56vO0Q/edit?usp=sharing
Here are the WIX JSON Guidelines:
If you get an error, check your structured data and make sure:
Your code is in the JSON-LD format.
Your code begins with the HTML tag and ends with .
Within the tag, the JSON-LD itself is wrapped in curly brackets { } .
Each element in the JSON-LD is separated from the next one with a comma. However, the last element shouldn't have a comma after it.
Your code is using straight quotes "". Curly quotes “” don't pass validation.
Your code is using only one script tag, and this script tag includes only one main schema markup type (one main JSON-LD, nesting is allowed).
All fields have the format: "key":"content". Pressing 'Enter' in either field won't pass validation, for example:
Pass validation: "text" : "this is a sentence with text."
FAIL validation: "text" : "this is a
sentence with text."
Your code contains less than 7,000 characters. Code with over 7,000 characters can not be added.
You have added multiple top level entities which is not a valid syntax. To get around it you need to define a top level entity that contains all the others. This is done by using #graph and listing the others in an array. e.g.
<script type="application/ld+json">
{
"#context": "http://schema.org",
"#graph": [
{
"#type": "TravelAgency",
"name": "ColombiaMotoAdventures",
"image": "https://static.wixstatic.com/media/331b1d_452c7e9b5e304d2ca37e16dc81141be6~mv2.png/v1/fill/w_270,h_270,al_c,usm_0.66_1.00_0.01/331b1d_452c7e9b5e304d2ca37e16dc81141be6~mv2.png",
"#id": "",
"url": "https://www.ColombiaMotoAdventures",
"telephone": "3214917060",
"priceRange": "80-100",
"address": {
"#type": "PostalAddress",
"streetAddress": "Cra. 102, Loma de Pajarito ##68-36",
"addressLocality": "Medellin",
"postalCode": "",
"addressCountry": "CO"
},
"openingHoursSpecification": {
"#type": "OpeningHoursSpecification",
"dayOfWeek": [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
],
"opens": "09:00",
"closes": "18:00"
},
"sameAs": "https://www.facebook.com/ColombiaMotoAdventures"
},
{
"#type": "WebSite",
"name": "Colombia Moto Adventures",
"url": "https://www.colombiamotoadventures.com/",
"potentialAction": {
"#type": "SearchAction",
"target": "https://www.colombiamotoadventures.com/?s={search_term_string}{search_term_string}",
"query-input": "required name=search_term_string"
}
},
{
"#type": "Trip",
"name": "Colombia Motorcycle Tour",
"description": "The dozen best places and areas that should be on your list when visiting Medellin, Colombia. Our motorcycle tours and rentals are geared towards beginner and intermediate riders. We have lots of people come down and tour around who don't speak a word of Spanish and they do just fine. Our tours are also geared towards beginner and intermediate riders. We are an American owned ADV motorcycle rental company based in Medellin, Colombia. Our motorcycles are perfect for comfortable, long-distance touring but are also capable to take you mud, rocks and river-crossings if that's where you decide to go.",
"itinerary": [
{
"#type": "Country",
"name": "Colombia",
"description": "Colombia is projected as one of Latin America's main destinations in terms of adventure tourism, multiple cultural activities, the best gastronomy, so that you can make your tourism trip an unforgettable experience. It is a country that makes you vibrate with its emotions, where you can choose the destination of your preference.",
"url": "https://en.wikipedia.org/wiki/Colombia"
}
]
},
{
"#type": "TouristTrip",
"name": "Colombia",
"description": "This trip is modeled as several distinct Tourist Trips using the subTrip property.",
"subTrip": [
{
"#type": "TouristTrip",
"name": "Colombia Jungle Motorcycle Adventure",
"description": "Rent a motorcycle in Medellin and drive to the jungle. Swim in the crystal clear waters of a jungle river and drink cold beers while listening to the amazing sounds of the jungle at night. The perfect South America motorcycle route"
},
{
"#type": "TouristTrip",
"name": "Guatape Motorcycle Trip",
"description": "Ride a motorcycle to Guatape and have lunch! Leave Medellín in the morning and take a drive out to the town of Guatape."
},
{
"#type": "TouristTrip",
"name": "Suzuki DR650 Motorcycle Rental",
"description": "Rent a DR650 motorcycle in Colombia. Known as the worlds best dual-sport motorcycle take your South America motorcycle adventure with this! "
},
{
"#type": "TouristTrip",
"name": "Medellin Motorcycle Trout Fishing Day Trip",
"description": "Ride an adventure motorcycle into the remote backcountry outside of Medellin and catch trout with a local family. "
},
{
"#type": "TouristTrip",
"name": "Cerro del Padre Amaya Off-Road Motorcycle Trip",
"description": "Boquerón to Padre Amaya - This is where things get interesting, We take our motorcycles and start heading up an off-road trail stunning views and high altitude."
},
{
"#type": "TouristTrip",
"name": "Colombia Waterfalls Motobike Tour",
"description": "Want to explore beyond Guatape? This motorcycle trip is for you! San Carlos is a small pueblo near Medellín that hasn’t yet been discovered by many foreigners. But it’s a hidden gem surrounded by mountains, rivers, waterfalls, and nature."
},
{
"#type": "TouristTrip",
"name": "Medellin Motorcycle Rental",
"description": "Motorcycle Tours and Rentals in Medellin, Colombia. We have the best selection of adventure motorcycles in Colombia Suzuki DR650, Honda XRE300, Kawasaki Versys-X 300."
},
{
"#type": "TouristTrip",
"name": "5 Day Colombia Lost Emerald City Motorcycle Tour",
"description": "Off-Road Colombia - Journey to the Lost Emerald City. Ride motorcycles off-road through rugged terrain, cross mountain ranges, and streams see amazing vistas and waterfalls and finally get the chance to and interact with local emerald miners. You can even buy emeralds directly from the miners in the field!"
},
{
"#type": "TouristTrip",
"name": "Colombia Coffee Region Motorcycle Tour",
"description": "Take an amazing motorcycle trip through Colombia's Coffee Region. Swim in the hot springs in Santa Rosa and take a coffee tour in Salento as well as walking through the Wax Palms in the Valle Cocora, and as always, enjoy all the twists and turns while riding through some of the amazing scenery in the world."
},
{
"#type": "TouristTrip",
"name": "19 Day Colombia to Ecuador Motorcycle Trip",
"description": "Journey To The Center Of The Earth. Based on customer feedback, we have decided to turn our most popular South America motorcycle route in Colombia into a full motorcycle tour through the most beautiful parts of Colombia and Ecuador. Rent a motorcycle in Medellin, Colombia, and ride to Quito, Ecuador and back."
}
]
}]
}
</script>

lJSON-LD Syntax error: value, object or array expected

I am just trying to get our events onto google using markup and I am fairly new to all of this so I hope someone can help I have just checked the following and got the error above
<script type="application/ld+json">
{
"#context": "https://schema.org",
"#type": "Event",
"name": “Staunton Harold Sportive”,
"startDate": "2019-05-26T08:00",
"endDate": "2019-05-26T17:00",
"location": {
"#type": "Place",
"name": “Staunton Harold Hall”,
"address": {
"#type": "PostalAddress",
"streetAddress": “Melbourne Road”,
"addressLocality": “Ashby De la Zouch“,
"postalCode": "LE65 1RT",
"addressRegion": “Derbyshire”,
"addressCountry": "UK”
}
},
"image": [
"https://static.wixstatic.com/media/36c4d1_84383c3423f0429b8fe7988a0382dbb0~mv2_d_3905_2194_s_2.png/v1/fill/w_1084,h_608,al_c,usm_0.66_1.00_0.01/Staunton%20Harold%20sportive.png",
],
"description": " 
Fun - 14K
Short - 40K
Medium - 100K
Long - 160K The Staunton Harold Sportive starts from the privately owned, estate in the stunning surroundings of Staunton Harold Hall  estate, providing the perfect backdrop for a fun and enjoyable bike ride.",
"offers": {
"#type": "Offer",
"url": "https://www.triathlon-events.com/staunton-harold-sportive",
"price": “20",
"priceCurrency": “GBP”,
"availability": "https://schema.org/InStock",
"validFrom": "2018-05-26T16:00"
},
“Organizer”: {
"#type": “Organizer”,
"name": “Choose Sports Ltd”
}
}
</script>
It could be because you have curly quotation marks in places instead of straight ones e.g. “Staunton Harold Sportive” needs to have the straight quotations marks as used in "https://schema.org". I'd start by swapping those out and retest. Hope that helps.

Adding JSON-LD for a list of strings

I'm trying to use Thymeleaf to output a list of strings for the ingredient field of my JSON-LD. In my controller I have recipe.ingredients which is a ArrayList<String>.
My code:
<script type="application/ld+json" th:inline="text">
{
"#context": "http://schema.org",
"#type": "Recipe",
"author": "",
"name": "[[ #{${item.name}} ]]",
"description": "[[ #{${item.shortDesc}} ]]",
"image": "[[ #{${item.image}} ]]",
"ingredients": [[ #{${recipe.ingredients}} ]],
}
Ingredient is output in the generated JSON-LD as (missing quotes around each element):
"ingredients": [2 eggs, 3 tbs almond flour, 1 tbs butter, 1/2 tsp baking powder, 2 pieces cooked bacon, 1 slice american cheese, Everything but the bagel seasoning],
How can I get a proper JSON array of strings from the List<String> in the model?

working with advavced json data anjular js

i am new to anjular js , i know how to work with basic json data with anjular js.
i have nauseated json data
[
{
"activity_user": "asd#gmail.com",
"home_id": "1",
"recent_connect_address": "South Hill Road, Callington, Cornwall ",
"recent_connect_postcode": "WA3 1PQ",
"propertyimg": "a.jpg",
"datemovein": "2014-12-04 00:00:00",
"datemoveout": "2016-12-29 00:00:00",
"list": "[{ comment:\"The $190 Bonavita 1900TS made better coffee than the other machines, according to our 10-person tasting panel. \", date:\"2014-12-01 00:00:00\"},{ comment:\"The $190 Bonavita 1900TS made better coffee than the other machines, according to our 10-person tasting panel. \", date:\"2014-12-01 00:00:00\"}]"
},
{
"activity_user": "asd525#gmail.com",
"home_id": "2",
"recent_connect_address": "548 Newton Road, Lowton, Warrington ",
"recent_connect_postcode": "PL17 7LH",
"propertyimg": "a.jpg",
"datemovein": "2014-12-01 00:00:00",
"datemoveout": "2014-12-31 00:00:00",
"list": "[{ comment:\"We considered 80 Champagne glasses before testing 10 glasses for 12 hours, and we found that the Schott Zwiesel 1872 Enoteca is best for most people. It’s taller, lighter, and thinner than any glass we tried, with tiny etching to keep Champagne carbonated longer. The tulip shape allows more aromas to reach your nose while still maintaining an elegant profile.\", date:\"2014-12-31 00:00:00\"}]"
}
]
now i want to print it out following format
<div class="row" ng-controller="ListCtrl">
<div ng-repeat="property in timeline" >
<div>{{property.activity_user}}</div>
<div class="comments">
<div>{{property.list.}}</div>
</div>
</div>
here is my controller but it not work
function ListCtrl($scope, $http) {
$http({method: 'GET', url: 'my.json'}).success(function(data) {
$scope.timeline = data;
});
};
i refer Accesing nested JSON with AngularJS but i didn't understand it
There are several problems in your code right now:
DEMO
The interpolated value inside your ng-repeat for the property.list has a dot in it:
change
<div>{{property.list.}}</div>
to
<div>{{property.list}}</div>
You are missing a div element in your html for closing the top level div.
You are declaring your controllers in a global manner, this is already deprecated and no longer recommended as of AngularJS 1.3. See documentation Arguments Section.
Instead of declaring it like this:
function ListCtrl() {}
You can do this instead:
angular.module('yourApp', [])
.controller('ListCtrl', function() {});
The list property is a string, not an array of objects representing comments.
I suggest you change its structure to something like this:
"list": [
{
"comment": "The $190 Bonavita 1900TS made better coffee than the other machines, according to our 10-person tasting panel. ",
"date":"2014-12-01 00:00:00"
},
{
"comment": "The $190 Bonavita 1900TS made better coffee than the other machines, according to our 10-person tasting panel. ",
"date":"2014-12-01 00:00:00"
}]
Javascript
angular.module('demo', [])
.controller('ListCtrl', function($scope, $http) {
$http({method: 'GET', url: 'my.json'}).success(function(data) {
$scope.timeline = data;
});
});
HTML
<div class="row" ng-controller="ListCtrl">
<div ng-repeat="property in timeline">
<div>{{property.activity_user}}</div>
<div class="comments">
<div ng-repeat="item in property.list">
<div>{{item.comment}}</div>
<em>-- {{item.date}}</em>
</div>
</div>
</div>
</div>
my.json
[
{
"activity_user": "asd#gmail.com",
"home_id": "1",
"recent_connect_address": "South Hill Road, Callington, Cornwall ",
"recent_connect_postcode": "WA3 1PQ",
"propertyimg": "a.jpg",
"datemovein": "2014-12-04 00:00:00",
"datemoveout": "2016-12-29 00:00:00",
"list": [
{
"comment": "The $190 Bonavita 1900TS made better coffee than the other machines, according to our 10-person tasting panel. ",
"date":"2014-12-01 00:00:00"
},
{
"comment": "The $190 Bonavita 1900TS made better coffee than the other machines, according to our 10-person tasting panel. ",
"date":"2014-12-01 00:00:00"
}
]
},
{
"activity_user": "asd525#gmail.com",
"home_id": "2",
"recent_connect_address": "548 Newton Road, Lowton, Warrington ",
"recent_connect_postcode": "PL17 7LH",
"propertyimg": "a.jpg",
"datemovein": "2014-12-01 00:00:00",
"datemoveout": "2014-12-31 00:00:00",
"list": [
{
"comment": "We considered 80 Champagne glasses before testing 10 glasses for 12 hours, and we found that the Schott Zwiesel 1872 Enoteca is best for most people. It’s taller, lighter, and thinner than any glass we tried, with tiny etching to keep Champagne carbonated longer. The tulip shape allows more aromas to reach your nose while still maintaining an elegant profile",
"date":"2014-12-31 00:00:0"
}
]
}
]
If you re using ( $scope.timeline ), that means you must use in html :
{{timeline[0].activity_user}} // print: asd#gmail.com