Golang equivalent of `jq --stream -c` - go

Using jq --stream -c on the command line, I can format pretty JSON like this:
{
"object": {
"something": {
"key1": 123,
"key2": 456
},
"something_else": {
"key1": [
"value1",
"value2"
]
}
}
}
into this:
[["object","something","key1"],123]
[["object","something","key2"],456]
[["object","something","key2"]]
[["object","something_else","key1",0],"value1"]
[["object","something_else","key1",1],"value2"]
[["object","something_else","key1",1]]
[["object","something_else","key1"]]
[["object","something_else"]]
[["object"]]
I've looked through the Golang documentation for JSON but couldn't find anything similar. Is there such a function that I've missed?
More precisely I'd like to print the above JSON like so:
object.something.key1=123
object.something.key2=345
object.something_else.key1.0=value1
object.something_else.key1.1=value2

I've looked through the Golang documentation for JSON but couldn't find anything similar. Is there such a function that I've missed?
No, you haven't missed anything. The current JSON library doesn't support what you described "out of the box".
If you want this to work, you'll need to find a package that offers the flexibility you need or satisfy json.Marshaler with a custom type yourself.

Related

Issues getting flow to send the correct json in body when using powerautomate's http request

I'm using a PowerAutomate Flow to call a native SmartSheet API that does a POST. The POST IS working but my MULTI_PICKLIST type field is not being populated correctly in SmartSheet due to the double quotes.
The API is: concat('https://api.smartsheet.com/2.0/sheets/', variables('vSheetID'), '/rows')
In the Body section of the http rest API I form my JSON and the section of interest looks like this:
{
"columnId": 6945615984781188,
"objectValue": {
"objectType": "MULTI_PICKLIST",
"values": [
#{variables('vServices')}
]
}
}
My variable vServices raw output looks like:
{
"body":
{
"name": "vServices",
"value": "Test1, Test2"
}
}
The format needs to be like this (it works using PostMan).
{
"columnId": 6945615984781188,
"objectValue": {
"objectType": "MULTI_PICKLIST",
"values": [
"Test1","Test2"
]
}
}
As a step in formatting my vServices variable I tried to do a replace function to replace the ',' with a '","' but this ultimately ends up as a \",\"
Any suggestion on how to get around this? Again, ultimately I need the desired JSON Body to read but haven't been able to achieve this in the Body section:
{
"columnId": 6945615984781188,
"objectValue": {
"objectType": "MULTI_PICKLIST",
"values": [
"Test1","Test2"
]
}
}
vs this (when using replace function).
{
"columnId": 6945615984781188,
"objectValue": {
"objectType": "MULTI_PICKLIST",
"values": [
"Test1\",\"Test2"
]
}
}
Thank you in advance,
I resolved my issue by taking the original variable, sending it to a compose step that did a split on the separator of a comma. I then added a step to set a new variable to the output of the compose step. This left me with a perfectly setup array in the exact format I needed! This seemed to resolve any of the issues I was having with double quotes and escape sequences.

How take exact 15characters using jsonata

Json:
{
"Data":"12345678"
}
I need the result like this
"Data": "12345678 "
Must take 15char
Please help me to write jsonata logic
$pad function will be useful here:
{
"Data": $pad(Data, 15)
}
You can check out this solution in Stedi's JSONata Playground: https://stedi.link/OM1K6XB

Rename JSON field in NiFi flow

I'm fairly new to NiFi, my question might be basic.
I would like to rename the JSON key in the flowfile. For example:
{"path":"/home/a/a", "size":"12345"}
and I would like to convert to
{"filename":"/home/a/a", "size":"12345"}
Tried using UpdateAttribute, adding a filename attribute with the value ${path} but either I'm doing something wrong or it's not meant to be used for this kind of operation.
How could I rename the attribute in a JSON ?
This is the content of your FlowFile, not an Attribute, so UpdateAttribute is not the right way to go.
The easiest way with JSON content of FlowFiles is going to be via a JOLTTransform.
Give this spec a try:
[
{
"operation": "shift",
"spec": {
"path": "filename",
"*": {
"#": "&"
}
}
}
]
You can test JOLT transforms here with input data and see what the output will be.

Parse response from a "folder items" request to find a file

Using the v2 of the box api, I use the folder items request to get information on files in a folder: http://developers.box.com/docs/#folders-retrieve-a-folders-items
I'm looking at trying to parse the response data. Any ideas how I can do this in bash to easily find a file in the user's account? I would like to find the name of the file where I can get the ID of the file as well.
response looks something like this:
{
"total_count": 25,
"entries": [
{
"type": "file",
"id": "531117507",
"sequence_id": "0",
"etag": "53a93ebcbbe5686415835a1e4f4fff5efea039dc",
"name": "agile-web-development-with-rails_b10_0.pdf"
},
{
"type": "file",
"id": "1625774972",
"sequence_id": "0",
"etag": "32dd8433249b1a59019c465f61aa017f35ec9654",
"name": "Continuous Delivery.pdf"
},
{ ...
For bash, you can use sed or awk. Look at Parsing JSON with Unix tools.
Also if you can use a programming language, then python can be your fastest option. it has a nice module json http://docs.python.org/library/json.html. It has a simple decode API which will give a dict as the output
Then
import json
response_dict = json.loads(your_response)
I recommend using jq for parsing/munging json in bash. It is WAY better than trying to use sed or awk to parse it.

What is the ideal JSON API format for Restkit integration?

Is there a model API I can watch/try, that has the easiest integration with RestKit (using the most defaults)?
I'm talking about the JSON format (having a root or not, nested objects, etc) and the expected answer after a POST for example...
The best resource I could find is this writeup here that teaches by example:
https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md
It would seem the basic format to represent a record from your model is something like:
{
"recordType": {
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3"
}
}
and if you have a collection of records, use Javascript array, like this: [ {...}, {...} ]
The writeup also suggests this form, which is:
{
"recordType": [
{
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3"
},
{
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3"
}
]
}
I guess you can map both ways.
This question may be helpful:
JSON format inconsistency

Resources