Search numbers between quotation marks and remove it (VS Code) - regex

I've a really big .json file with this structure:
[
"9": {
"id": 9,
"chapter": 4
},
"4": {
"id": 82,
"chapter": 32
},
]
I want to remove every quotation marks before the brackets. The file should looking like this:
[
{
"id": 9,
"chapter": 4
},
{
"id": 82,
"chapter": 32
},
]
I found a similar question here, but the result isn't that what I want.

this step will remove keys; what are looking for( assuming no nested objects ) :
"\d+":\s*{ replace with '{'
this will remove latest ,if exist and turn last } to ]
},?\s*}$ replace with '}]'
optional step: this will remove latest ,if exist
},?\s*]$ replace with '}]'
check code snippet below for working example from your input to a valid json:
copy the pattern between // and paste it to vs-code find bar; (e.g. /},?\s*]$/ will be },?\s*]$) to be used in VSCode and the likewise for the replace
const input = `[
"9": {
"id": 9,
"chapter": 4
},
"4": {
"id": 82,
"chapter": 32
},
]`
const step1Find = /"\d+":\s*{/g;const step1Replace = '{'
//not needed anymore
//const step2Find = /^{/;const step2Replace = '['
//removing lastest comma if exists
const step3Find = /},?\s*]$/;const step3Replace = '}]'
let stringObject = input
.replace(step1Find,step1Replace)
.replace(step3Find,step3Replace)
console.log(JSON.parse(stringObject))

Related

CARBONE.IO Conditional display (ifEQ)

I'm using Carbone to generate PDF with variables.
I need to make a conditional display. If a product have a specific category I need to display an information.
I tried to make the condition with ifEQ, show and showBeggin // showEnd but it does'nt work.
// My JSON
const json = {
shopping : {
sales :[
{
product_name: "Apple",
product_price: 2,
product_category: "fruits",
comment: "",
},
{
product_name: "Coke",
product_price: 3,
product_category: "soda",
comment: "",
},
{
product_name: "Cucumber",
product_price: 1.5,
product_category: "vegetable",
comment: "",
},
{
product_name: "Vodka",
product_price: 15,
product_category: "Alcohol",
comment: "Dangerous for health, prohibited at least 18 years old",
},
]
}
}
What I made on my document :
State
{d.shopping.sales[i].product_name} {d.shopping.sales[i].product_category(“Alcohol”):showBeggin} Information {d.shopping.sales[i].comment}, {d.shopping.sales[i].product_price}, {d.shopping.sales[i].produt_name}{d.shopping.sales[i].product_category::showEnd}
{d.shopping.sales[i+1].product_name}
Thank's for help !
Your example got a couple of errors, some fixes to do:
it is missing the ifEQ just before the showBegin
if the ifEQ argument is a string, it must be wrap with single quotes
showBeggin should be written showBegin
the last showEnd should be preceded by only one colon.
Here is your template with all corrections:
State
{d.shopping.sales[i].product_name} {d.shopping.sales[i].product_category:ifEQ('Alcohol'):showBegin}
Information {d.shopping.sales[i].comment}, {d.shopping.sales[i].product_price}, {d.shopping.sales[i].produt_name}{d.shopping.sales[i].product_category:showEnd}
{d.shopping.sales[i+1].product_name}
The conditional block documentation has been improved a lot, to get more examples: https://carbone.io/documentation.html#conditioned-output
Have a great day!

Inserting string into specific part of a javascript file

I need to change one specific section of a javascript file by inserting a string, with python. Here is a small sample of the javascript file.
//----------------------------------------Peter Lighting Priorities-----------------------------------------------------
/*
// Lighting low prios for everything else//
peter_RenderBoy_lighting_lowPrios = {
"fromProductionGroups" : ["peter_RenderBoy_lighting"],
"matchFilters" : [
{"user": "", "name": ""}
],
"toProductionGroup" : "",
"priority" : 3
};
moveAndPrioritise(peter_RenderBoy_lighting_lowPrios);
// lighting normal and Approved LFL shots (emailed at 8pm) //
peter_RenderBoy_lighting_normalPrios = {
"fromProductionGroups" : ["peter_RenderBoy_lighting"],
"matchFilters" : [
{"user": "", "name": ""}
],
"toProductionGroup" : "",
"priority" : 2
};
moveAndPrioritise(peter_RenderBoy_lighting_normalPrios);
Here is the same sample of javascript showing the string "INSERT_STRING_HERE" in bold where it needs to be inserted:
//----------------------------------------Peter Lighting Priorities-----------------------------------------------------
/*
// Lighting low prios for everything else//
peter_RenderBoy_lighting_lowPrios = {
"fromProductionGroups" : ["peter_RenderBoy_lighting"],
"matchFilters" : [
{"user": "", "name": ""}
],
"toProductionGroup" : "",
"priority" : 3
};
moveAndPrioritise(peter_RenderBoy_lighting_lowPrios);
// lighting normal and Approved LFL shots (emailed at 8pm) //
peter_RenderBoy_lighting_normalPrios = {
"fromProductionGroups" : ["peter_RenderBoy_lighting"],
"matchFilters" : [
{"user": "", "name": "***INSERT_STRING_HERE***"}
],
"toProductionGroup" : "",
"priority" : 2
};
moveAndPrioritise(peter_RenderBoy_lighting_normalPrios);
This was my attempt so far, which merely shows that I've imported regex, but since gave up on my efforts with that, and am trying to loop through the javascript file, but don't know how to proceed to achieve what I need:
import re
javascript_file = open('/depts/wranglers/frankie/farmtools/priorities/autoHook.js')
for line in javascript_file:
if '// lighting normal and Approved LFL shots (emailed at 8pm) //' in line:
When you see that comment line, set a variable. Then when you see the line with the pattern you want to replace, check whether the variable is set, and do the replacement.
with open('/depts/wranglers/frankie/farmtools/priorities/autoHook.js') as javascript_file, \
open('/depts/wranglers/frankie/farmtools/priorities/autoHook.new.js') as new_file
section_found = False
for line in javascript_file:
if '// lighting normal and Approved LFL shots (emailed at 8pm) //' in line:
section_found = True
elif '{"user": "", "name": ""}' and section_found:
line = line.replace('"name": ""', '"name": "INSERT_STRING_HERE"')
section_found = False
new_file.write(line + "\n")

autofilling a dict python 2.x

I'm quite new to Python and programming in general, so apologies if this is quite basic or has been asked and answered before. Here is a sample of the data I'm working with:
{
"homeTeam": {
"formation": [
"4",
"4",
"2"
],
"lineupsSorted": [
{
"player": {
"name": "Scott P. Brown",
"slug": "scott-p-brown",
"shortName": "S. P. Brown",
"id": 19889,
"hasImage": true
},
"position": 1,
"shirtNumber": 1,
"substitute": false,
"positionName": "Goalkeeper",
"positionNameshort": "G",
"captain": false
},
{
"player": {
"name": "Carl Winchester",
"slug": "carl-winchester",
"shortName": "C. Winchester",
"id": 110785,
"hasImage": true
},
"position": 2,
"shirtNumber": 27,
"substitute": false,
"positionName": "Midfielder",
"positionNameshort": "M",
"captain": false
},
I am looking to automate populating defined names as I have done manually here:
hometeamPositions =['Goalkeeper','Midfielder','Defender','Defender','Defender','Midfielder','Midfielder','Midfielder','Midfielder','Forward','Forward','Goalkeeper','Defender','Defender','Midfielder','Midfielder','Forward','Forward']
hometeamPlayers = ['S. P. Brown','C. Winchester','M. Onariase','W.
Boyle','J. Cranston','H. Pell','J. Rowe','K. Storer','B. Waters','D.
Wright','D. Holman','R. Lovett','J. Barthram','T. Plavotic','J.
Munns','L. Davis','K. Wootton','J. Dayton']
As I will be repeating this process many hundreds of times with different data (same structure) I was wondering if anyone could give me some tips on automatically building these ranges?
Thanks,
Peter
I'm not sure I understood what is the problem you are trying to solve but I'll try to help.
Assuming you have a dictionary team_dict and you want to create 2 list: hometeamPositions and hometeamPlayers you can use the following code:
hometeamPlayers = []
hometeamPositions = []
for player_dict in teams_dict['homeTeam']['lineupsSorted']:
hometeamPlayers.append(player_dict['player']['shortName'])
hometeamPositions.append(player_dict['positionName'])
The output on your example will be:
hometeamPlayers = ['S. P. Brown', 'C. Winchester']
hometeamPositions = ['Goalkeeper', 'Midfielder']

Sort documents based on first character in field value

I have a set of data like this:
[{name: "ROBERT"}, {name: "PETER"}, {name: "ROBINSON"} , {name: "ABIGAIL"}]
I want to make a single mongodb query that can find:
Any data which name starts with letter "R" (regex: ^R)
Followed by any data which name contains letter "R" NOT AS THE FIRST CHARACTER, like: peteR, adleR, or caRl
so it produces:
[{name: "ROBERT"}, {name: "ROBINSON"}, {name: "PETER"}]
it basically just display any data that contains "R" character in it but I want to sort it so that data with "R" as the first character appears before the rest
So far I've come out with 2 separate query then followed by an operation to eliminate any duplicated results, then joined them. So is there any more efficient way to do this in mongo ?
What you want is add a weight to you documents and sort them accordingly.
First you need to select only those documents that $match your criteria using regular expressions.
To do that, you need to $project your documents and add the "weight" based on the value of the first character of your string using a logical $condition processing.
The condition here is $eq which add weight 1 to the document if the lowercase of the first character in the name is "r" or 0 if it's not.
Of course the $substr and the $toLower string aggregation operators respectively return the the first character in lowercase.
Finally you $sort your documents by weight in descending order.
db.coll.aggregate(
[
{ "$match": { "name": /R/i } },
{ "$project": {
"_id": 0,
"name": 1,
"w": {
"$cond": [
{ "$eq": [
{ "$substr": [ { "$toLower": "$name" }, 0, 1 ] },
"r"
]},
1,
0
]
}
}},
{ "$sort": { "w": -1 } }
]
)
which produces:
{ "name" : "ROBERT", "w" : 1 }
{ "name" : "ROBINSON", "w" : 1 }
{ "name" : "PETER", "w" : 0 }
try this :
db.collectioname.find ("name":/R/)

Creating a Sublime Text 3 Custom Syntax - matching words after keyword

I'm trying to create some simple custom highlighting in Sublime Text 3 using AAAPackageDev.
In the line
void plastic my_material,
I'd like to match plastic, i.e. the material type, as a keyword and my_material, i.e. the material name, as a string.
I've got the following which however only matches plastic and not the name that follows, i.e. my_material:
// [PackageDev] target_format: plist, ext: tmLanguage
{ "name": "Radiance",
"scopeName": "source.radiance",
"fileTypes": ["rad", "sky", "mat"],
"uuid": "885b4450-d2bb-4a67-aea0-ceb5a0554630",
"patterns": [
{
"name": "keyword.source.radiance",
"match": "\\bplastic\\b | \\blight\\b"
},
{
"name": "keyword.source.radiance",
"begin": "(\\bplastic\\b | \\blight\\b)",
"patterns": [
{ "include": "$self"},
{
"name": "string.source.radiance",
"match": "[^\\s]+"
}],
"end": "\\n"
}]
}
I'm trying to follow these docs here. Any ideas?
EDIT
The full text I'm trying to parse is:
void plastic my_material
0
0
5 0.4 0.25 0.05 0.05 0.03
void and plastic maintain position but might change their value. my_material is just an arbitrary name that could comprise pretty much anything but must follow a keyword from the first pattern and will be followed by a new line.