format a json key-value pair's first field - json

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.

Related

How to make string replacement in a JSON template?

If I have a JSON template in which some variable should be replaced with their actual value, is there a good way to handle the proper escape?
For example, $value may be replaced with a string that contains characters like " that should be treated specially in JSON.
{ "x": $value }
The template could be arbitrary complex. So it is not a good solution to code the template in some programming language like python, then perform the replacement in that language, then dump the JSON output.
Could anybody show me a generic but succinct way to perform the replacement?
Note that I tagged this question with jq. I am not sure it is strictly relevant to this question. If not, please remove the tag. I tagged jq because people who know jq may also know solutions to my question, although jq is just for transforming a JSON file. An elegant solution may be similar to jq in the sense that a domain-specific language is defined.
jq works quite nicely as a template engine, but there are choices to be made, e.g. depending on whether the "template" itself is valid JSON.
In the example you gave, the template is not valid JSON, but it is potentially valid jq, so the strategy using jq "$-variables" would make sense, e.g. along the lines of:
jq -n --arg value "someValue" -f template.jq
where template.jq is your template.
Three different strategies for using jq as a template engine are explained at some length in the jq Cookbook:
https://github.com/stedolan/jq/wiki/Cookbook#using-jq-as-a-template-engine

Issues with JSON format while using jq commandline processor [duplicate]

This question already has an answer here:
jq special characters in nested keys
(1 answer)
Closed 3 years ago.
I am trying to use the jq command line JSON processor https://shapeshed.com/jq-json/ (which works great) to process a JSON file that seems to have been made using some poor choices.
Normally your id and value in the JSON file would not contain any periods such as:
{"id":"d9s7g9df7sd9","name":"Tacos"}
To get Tacos from the file you would do the following in bash:
echo $json | jq -r '.name'
This will give you Tacos (There may be some extra code missing from that example but you get the point.)
I have a JSON file that looks like this:
{"stat.blah":123,"stat.taco":495,"stat.yum... etc.
Notice how they decided to use a period in the identifying field associated with the value? This makes using jq very difficult because it associates the period as a separator to dig down into child values in the JSON. Sure, I could first load my file, replace all "." with "_" and that would fix the problem, but this seems like a really dumb and hackish solution. I have no way to change how the initial JSON file is generated. I just have to deal with it. Is there a way in bash I can do some special escape to make it ignore the period?
Thanks
Use generic object index syntax, e.g:
.["stat.taco"]
If you use the generic object syntax, e.g. .["stat.taco"], then chaining is done either using pipes as usual, or without the dot, e.g.
.["stat.taco"]["inner.key"]
If your jq is sufficiently recent, then you can use the chained-dot notation by quoting the keys with special characters, e.g.
."stat.taco"."inner.key"
You can also mix-and-match except that expressions such as: .["stat.taco"].["inner.key"] are not (as of jq 1.6) supported.

JSON Replace Text with variable

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

I need to remove a piece of every line in my json file

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.

Formatting json properties

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