I have a JSON File in which are Dateobjects. I want to replace them with Formated Date Strings. Can somebody give me a hint how it would be possible in an easy way?
In the JSON File there are lines like:
"insertedAt": { "$date": "2018-01-31T11:05:39.447Z" },
I want to replace them with:
"insertedAt": "2018-01-31 11:05:39.447000"
The files are pretty big so I would like to avoid loading them into JSON-Objects and solve it with regex replacement or something similar. Unfortunatly I am not really experienced with it.
Is it possible to replace something with a function output of itself?
I can select the parts that I want to replace with the regex:
"insertedAt":{".*"}
Can I now replace it with a function of *? In Pseudocode it would be like:
replace("\"insertedAt\":{\".*\"}").with("\"insertedAt\":" + format(*))
Any other ideas?
If you can use lookbehind in your regex engine, then you can try this for matching target string
(?<=insertedAT\": )\{ \"\$date\": (\"[^\"]*\") \},
and replace it with capture group 1 ( \1 or $1). Demo
Or, if you can't use lookbehind in your regex engine, then you can try this regex,
(?!insertedAT\": )\{ \"\$date\": (\"[^\"]*\") \},
Demo
Related
My CSV files with whitespace:
Id ; FirstName ; LastName ; email
123; Marc ; TOTO ; marc#toto.com
I would like delete whitespace in my csv by line like this :
Id;FirstName;LastName;email
123;Marc;TOTO;marc#toto.com
I would use a regex in Perl.
It is always a good idea to use a library with file formats like CSV. Even as this case seems trivial and safe to parse with regex surprises can and do sneak up. Also, requirements tend to change and projects and data only get more complex. Once there is sensible code using a good library a project evolution is generally far more easily absorbed.
A library like the excellent Text::CSV can use ; as a separator and can remove that extra whitespace while parsing the file, with a suitable option.
To keep it short and in a one-liner the functional interface is helpful
perl -MText::CSV=csv -we'
csv (in => *ARGV, sep => ";", allow_whitespace => 1)' name.csv > new.csv
Prints as desired with the supplied example file.
Not a perl solution but the awk solution is so simple it might be acceptable:
awk '{OFS="";$1=$1;print $0}' file.csv;
This process uses OFS to override the default output field separator from the usual white space. $1=$1 forces awk to reset the whole line $0 value to remove the field separators before printing it.
Although your title says remove spaces at the end of each line, you may
want to remove whitespaces around the field values. Then would you please try:
perl -pe "s/\s*;\s*/;/g" input_file.csv
Output:
Id;FirstName;LastName;email
123;Marc;TOTO;marc#toto.com
Please note the code breaks in case the field contains ; itself such as abc;"foo ; bar";def or other complicated cases.
I have a json output I'd like to convert to a Zabbix parameter.
Basically, I need to transform something like the following:
"key":"value"
into something like this:
"#{KEY}":"value"
Leaving everything else untouched.
That is, I need to convert the first field to uppercase, surround it with square brackets and add a hash before the first bracket.
I've tried some combinations of sed, awk and grep, but things quickly got too long and messy to do so effectively.
I'm really unfamiliar with json and sparesly deal with it - I'd really appreciate any pointers.
Thanks!
EDIT: link below has solved this. To clarify, this is from a bash environment.
I am facing issues trying to form regex of the following string.
[{"Column1":"Value1"},{"Column2":"Value2"},{"Column3":"Value3"},{"Column1":"Value4"},{"Column2":"Value5"},{"Column3":"Value6"},{"Column1":"Value7"},{"Column2":"Value8"},{"Column3":"Value9"}]
and I want output as
[{"Column1":"Value1","Column2":"Value2","Column3":"Value3"},{"Column1":"Value4","Column2":"Value5","Column3":"Value6"},{"Column1":"Value7","Column2":"Value8","Column3":"Value9"}]
I tried with (?!^)^(?=(?:[^"]*"[^"]*")*[^"]*$)|\}(?!$)(?=(?:[^"]*"[^"]*")*[^"]*$) but it is either removing all opening braces or all closing braces.
This output formation, is it at all possible from regex ? or are there any other options except split, remove, replace string?
You can use a regex to replace each group of three, and use capture groups so you can recompose the JSON with just the data you need.
var input = #"[{""Column1"":""Value1""},{""Column2"":""Value2""},{""Column3"":""Value3""},{""Column1"":""Value4""},{""Column2"":""Value5""},{""Column3"":""Value6""},{""Column1"":""Value7""},{""Column2"":""Value8""},{""Column3"":""Value9""}";
var re = new System.Text.RegularExpressions.Regex(#"(\{[^}]+)\},\{([^}]+)\},\{([^}]+)\}");
var output = re.Replace(input, "$1, $2, $3 }");
Console.WriteLine(output);
It is probably best to use Newtonsoft.JSON to parse and rebuild your JSON, otherwise if the format of the string isn't followed exactly as described then this solution will break. You can accommodate some variance by adding \s* before and after each brace so that whitespace is accounted for, but otherwise this is a brittle solution.
I have a json output on my notepad and i know it is not in the correct format. At the end of each line there is a time stamp which is causing the bad format. I want to get rid of it using find and replace since the file is pretty big. The format is as follows :
"eventtimestamp": "05 23 2017 04:01:02"}
The above piece comes in at the end of every line. How can i get rid of it using find a replace or any other way.
All help is appreciated.
Thank you
If you need to alter every line in a consistent way then regex find/replace is a good option. Free tools like atom.io, Notepad++, and plenty of others offer this feature.
Assuming "eventtimestamp" is constant, then a simple regex that says "find everything starting with "eventtimestamp" and up to a '}'" will work.
"eventtimestamp".*(?=})
And "replace" that with an empty string.
ps) here's a demo of the regex in regexr.com--hovering over the parts of the pattern will explain what they do.
If you are not sure that the eventtimestamp field always comes in at the end of a line and/or as the last element of the object, prefer that kind of pattern: "eventtimestamp":\s*"[^"]+",?.
Note the useful surrounded excepted character class pattern "[^"]+" that can be declined with any other delimiter.
I have a JSON file with thousands of lines, which i need to have in another format.
My current code looks like that:
[{"Date":"2012-05-11","Value":19.5},
{"Date":"2012-05-15","Value":19.5},
{"Date":"2012-05-16","Value":18},
{"Date":"2012-05-17","Value":17.75},
...]
And it should look like this:
[Date.UTC(2012,5,11),19.5],
[Date.UTC(2012,5,13),19.5],
[Date.UTC(2012,5,16),18],
[Date.UTC(2012,5,17),17.75],
....]
Is there a tool or a quick way to do this, changing thousands of lines manually would take too much time.
Thanks in advance.
Programmatically, say php for instance, and treating each line of your JSON file as a string, you could use preg_replace(), but since it's a one time requirement, simply load your json file into a text editor and:
Delete the leading and ending square braces.
Find all {"Date":" replace with [Date.UTC(
Find all ","Value": replace with ),
Find all }, replace with ],
Save