One of the elements of the array as an object - json

The question is how to get the "from" element? The rest is not a problem
I know that in https://github.com/json-iterator/ it can be done, but I could not figure out how it works there
Json:
{
"ab": 123456789,
"cd": [
[
4,
1234,
123456,
1000000001,
1234567890,
"text",
{
"from": "123456"
}
],
[
4,
4321,
654321,
1000000001,
9876543210,
"text",
{
"from": "654321"
}
]
]
}
Golang:
type test struct {
Ab int `json:"ab"`
Cd [][]interface{} `json:"cd"`
}
var testvar test
json.Unmarshal(Data, &testvar)
testvar.Cd[0][6]["from"].(string)
Error:
invalid operation: testvar.Cd[0][6]["from"] (type interface {} does not support indexing)

It's simple: it is a map[string]interface{} hence
m, ok := testvar.Cd[0][6].(map[string]interface{})
fmt.Println(m, ok, m["from"])
https://play.golang.org/p/XJGs_HsxMfZ

Related

hcl, json, go : how can I iterate JSON?

Problem: I’m trying to iterate through JSON-content and present the result like key,value pairs.
I've written some code that read hcl-files, these are then decoded with hcldec.Decode, and the result is then converted to JSON. These hcl-files define source and target for the application like this:
source.hcl:
source json "namefile" {
attr firstName {
type = "varchar"
expr = "$.firstName"
length = "30"
}
attr lastName {
type = "varchar"
expr = "$.lastName"
length = "40"
}
attr gender {
type = "varchar"
expr = "$.gender"
length = "10"
}
attr age {
type = "varchar"
expr = "$.age"
length = "2"
}
}
target.hcl
target table {
cols firstName {
name=source.json.namefile.attr.firstName.expr
type=source.json.namefile.attr.firstName.type
length=source.json.namefile.attr.firstName.length
}
cols lastName {
name=source.json.namefile.attr.lastName.expr
type=source.json.namefile.attr.lastName.type
length=source.json.namefile.attr.lastName.length
}
}
The decoding is done like this:
tspec := hcldec.ObjectSpec{
"target": &hcldec.BlockMapSpec{
TypeName: "target",
LabelNames: []string{"table"},
Nested: hcldec.ObjectSpec{
"cols": &hcldec.BlockMapSpec{
TypeName: "cols",
LabelNames: []string{"name"},
Nested: &hcldec.ObjectSpec{
"name": &hcldec.AttrSpec{
Name: "name",
Type: cty.String, //cty.List(cty.String),
Required: false,
},
"type": &hcldec.AttrSpec{
Name: "type",
Type: cty.String, //cty.List(cty.String),
Required: false,
},
"length": &hcldec.AttrSpec{
Name: "length",
Type: cty.String, //cty.List(cty.String),
Required: false,
},
},
},
},
},
}
targ, _ := hcldec.Decode(body, tspec, &hcl.EvalContext{
Variables: map[string]cty.Value{
"source": val.GetAttr("source"),
},
Functions: nil,
})
j := decodeCtyToJson(targ, true)
log.Debugf("targ -j (spec): %s", string(j)) // debug info
Where the decodeCtyToJson return []byte like this:
func decodeCtyToJson(value cty.Value, pretty bool) []byte {
jsonified, err := ctyjson.Marshal(value, cty.DynamicPseudoType)
if err != nil {
log.Debugf("Error: #v", err)
return nil
}
if pretty {
return jsonPretty.Pretty(jsonified)
}
return jsonified
}
Now, when I'm trying to testprint the JSON-content I'm not getting what I'm looking for:
var result map[string]interface{}
json.Unmarshal(j, &result)
log.Debugf("result: %# v", result)
tgtfil := result["value"].(map[string]interface{})
log.Debugf("tgtfil: %v", tgtfil)
log.Debugf("len(tgtfil): %# v", len(tgtfil))
for key, value := range tgtfil {
log.Debugf("key: %# v", key)
log.Debugf("value: %# v", value)
}
I am trying to get key, value pairs. But I'm getting this (first the whole JSON pretty print as wanted, then I am trying to loop through the JSON):
DEBU[0000] targ -j (spec): {
"value": {
"target": {
"table": {
"cols": {
"firstName": {
"length": "30",
"name": "$.firstName",
"type": "varchar"
},
"lastName": {
"length": "40",
"name": "$.lastName",
"type": "varchar"
}
}
}
}
},
"type": [
"object",
{
"target": [
"map",
[
"object",
{
"cols": [
"map",
[
"object",
{
"length": "string",
"name": "string",
"type": "string"
}
]
]
}
]
]
}
]
}
DEBU[0000] result: map[string]interface {}{"type":[]interface {}{"object", map[string]interface {}{"target":[]interface {}{"map", []interface {}{"object", map[string]interface {}{"cols":[]interface {}{"map", []interface {}{"object", map[string]interface {}{"length":"string", "name":"string", "type":"string"}}}}}}}}, "value":map[string]interface {}{"target":map[string]interface {}{"table":map[string]interface {}{"cols":map[string]interface {}{"firstName":map[string]interface {}{"length":"30", "name":"$.firstName", "type":"varchar"}, "lastName":map[string]interface {}{"length":"40", "name":"$.lastName", "type":"varchar"}}}}}}
DEBU[0000] tgtfil: map[target:map[table:map[cols:map[firstName:map[length:30 name:$.firstName type:varchar] lastName:map[length:40 name:$.lastName type:varchar]]]]]
DEBU[0000] len(tgtfil): 1
DEBU[0000] key: "target"
DEBU[0000] value: map[string]interface {}{"table":map[string]interface {}{"cols":map[string]interface {}{"firstName":map[string]interface {}{"length":"30", "name":"$.firstName", "type":"varchar"}, "lastName":map[string]interface {}{"length":"40", "name":"$.lastName", "type":"varchar"}}}}
Process finished with exit code 0
My aim here is to eventually be able to iterate through alle the attributes defined in the target.hcl (length, name and type for each cols in this case). Then generate DDL-code from this information and finally implement the DDL in e.g. Presto.
But as of now I’m not able to isolate this information.
Any pointers on how to do this is appreciated.
Thanks,
/b
The solution for me was to create a struct for the target and not use the target spec.

Enumerate slice entries

If I have this output
Stuff {
"Items": [
{
"title": "test1",
"id": "1",
},
{
"title": "test",
"id": "2",
},
],
"total": 19
},
}
But want this instead:
stuff {
"Items": [
1:{
"title": "test1",
"id": "1",
},
2:{
"title": "test",
"id": "2",
},
],
"total": 19
},
}
Currently, my structs are build like this:
Stuff struct {
Items []Items `json:"items"`
Total int `json:"total"`
} `json:"stuff"`
type Items struct {
Title string `json:"title"`
ID string `json:"id"`
}
I initiate a slice by:
stuff := make([]Items, 10) // Lets say I have 10 entries
And append by:
Stuff.Items = stuff
Stuff.Total = len(Stuff.Items)
Now I am unsure on how to numerate that. So for every item entries, there should be a number, starting from 1 - 10 (In this example)
Given your Stuff and Items type declarations, here's a simple data structure and its JSON dump:
s := Stuff{Items: []Items{Items{"test1", "1"}, Items{"test2", "2"}}, Total: 10}
j, err := json.MarshalIndent(s, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(j))
JSON:
{
"items": [
{
"title": "test1",
"id": "1"
},
{
"title": "test2",
"id": "2"
}
],
"total": 10
}
To see what you want instead, there's no magic json package call to do that. You'll have to create a new data structure that reflects the structure of your desired output.
In this case a simple map will do:
m := make(map[string]Items)
for _, item := range s.Items {
m[item.ID] = item
}
And now if you JSON-dump this map, you get:
{
"1": {
"title": "test1",
"id": "1"
},
"2": {
"title": "test2",
"id": "2"
}
}
Note that I'm not wrapping it with Stuff now, because Stuff has different fields. Go is statically typed, and each struct can only contain the fields you declared it to have.

How can I use serde json on a json object with variable key names

I am aware of the JSON value which can be used for unknown JSON.
What I have is a mostly structured JSON object like this:
{
"error": [],
"result": {
"NAME_X": {
"prop_one": "something",
"prop_two": "something",
"decimals": 1,
"more_decimals": 2
},
"NAME_A": {
"prop_one": "test",
"prop_two": "sdfsdf",
"decimals": 2,
"more_decimals": 5
},
"ARBITRARY": {
"prop_one": "something else",
"prop_two": "blah",
"decimals": 3,
"more_decimals": 6
}
}
So the inside object with fields prop_one, prop_two, decimals and more_decimals has a clear structure, but the outer name field/key (NAME_X, NAME_A, ARBITRARY) is unknown in advance.
What is the most straight forward way to parse this so I can use strongly typed variables/deserialization on the inner structure? I also still need to capture those unknown name fields.
You can deserialize into a map whose keys will be strings ("NAME_X", etc.):
use std::collections::HashMap;
use serde::Deserialize;
use serde_json::Result;
#[derive(Debug, Deserialize)]
struct InThing {
prop_one: String,
prop_two: String,
decimals: u16,
more_decimals: i32,
}
#[derive(Debug, Deserialize)]
struct OutThing {
error: Vec<u8>,
result: HashMap<String, InThing>,
}
fn main() {
let data = r#"
{
"error": [],
"result": {
"NAME_X": {
"prop_one": "something",
"prop_two": "something",
"decimals": 1,
"more_decimals": 2
},
"NAME_A": {
"prop_one": "test",
"prop_two": "sdfsdf",
"decimals": 2,
"more_decimals": 5
},
"ARBITRARY": {
"prop_one": "something else",
"prop_two": "blah",
"decimals": 3,
"more_decimals": 6
}
}
}
"#;
let thing: OutThing = serde_json::from_str(data).unwrap();
dbg!(thing);
}
playground

Get value JSON to create a nested in golang

I'm trying to create dynamically nested json from value FIELD_QUESTION and FIELD_ANSWER in golang.
my result json
{
"jumlahdata": 2,
"result": [
{
"QUICK_DATA_H_ID": "1",
"ORDER_TRX_H_ID": "1",
"FIELD_QUESTION": "FULLNAME",
"FIELD_ANSWER": "RUBEN",
"DTM_CRT": "2019-08-28T16:25:15.757Z"
},
{
"QUICK_DATA_H_ID": "2",
"ORDER_TRX_H_ID": "1",
"FIELD_QUESTION": "ALAMAT_KTP",
"FIELD_ANSWER": "jalandisana",
"DTM_CRT": "2019-08-28T16:25:15.757Z"
}
],
"statusdb": 200,
"statusload": 200,
"statusquery": 200
}
expected result nested
{
"jumlahdata": 1,
"result": [
{
"QUICK_DATA_H_ID": "1",
"ORDER_TRX_H_ID": "1",
"QUESTION":[{
"FULLNAME": "RUBEN",
"ALAMAT_KTP": "jalandisana",
}]
"DTM_CRT": "2019-08-28T16:25:15.757Z"
}
],
"statusdb": 200,
"statusload": 200,
"statusquery": 200
}
can anyone help with this? Please let me know if I haven't been clear enough!
maybe this tool can help you JSON-to-Go - https://mholt.github.io/json-to-go/
type AutoGenerated struct {
Jumlahdata int `json:"jumlahdata"`
Result []struct {
QUICKDATAHID string `json:"QUICK_DATA_H_ID"`
ORDERTRXHID string `json:"ORDER_TRX_H_ID"`
QUESTION []struct {
FULLNAME string `json:"FULLNAME"`
ALAMATKTP string `json:"ALAMAT_KTP"`
} `json:"QUESTION"`
DTMCRT time.Time `json:"DTM_CRT"`
} `json:"result"`
Statusdb int `json:"statusdb"`
Statusload int `json:"statusload"`
Statusquery int `json:"statusquery"`
}
For that, personally i love to use gabs module which allows to handle those kind of situation in a more human friendly way.
For installing the module use:
go get github.com/Jeffail/gabs/v2
So take your json string and pass it to gabs.ParseJSON as follows
// jsonParsed var contains a set of functions to play arround
jsonParsed, _ := gabs.ParseJSON([]byte(`{
"outter":{
"inner":{
"value1":10,
"value2":22
},
"alsoInner":{
"value1":20,
"array1":[
30, 40
]
}
}
}`))
// you can check for keys existence
exists := jsonParsed.Exists("outter", "inner", "value1")
// exists == true
// you can pretty print your json
fmt.Println(jsonObj.StringIndent("", " "))
// you can search for specific values in your inner object
value, ok = jsonParsed.Search("outter", "inner", "value1").Data().(float64)
// value == 10.0, ok == true
// and much more ...
Take a look at the documentation

Create struct for complex JSON array in Golang

I have the following JSON array that I'm trying to convert to a struct.
[
{
"titel": "test 1",
"event": "some value",
"pair": "some value",
"condition": [
"or",
[
"contains",
"url",
"/"
]
],
"actions": [
[
"option1",
"12",
"1"
],
[
"option2",
"3",
"1"
]
]
}, {
"titel": "test 2",
"event": "some value",
"pair": "some value",
"condition": [
"or",
[
"contains",
"url",
"/"
]
],
"actions": [
[
"option1",
"12",
"1"
],
[
"option2",
"3",
"1"
]
]
}
]
This is the struct I've got so far:
type Trigger struct {
Event string `json:"event"`
Pair string `json:"pair"`
Actions [][]string `json:"actions"`
Condition []interface{} `json:"condition"`
}
type Triggers struct {
Collection []Trigger
}
However, this does not really cover the "Condition" part. Ideally id like to have a structure for that as well.
Assuming that there can be only a single condition per item in the root array, you can try a struct below. This could make using Condition clear.
https://play.golang.org/p/WxFhBjJmEN
type Trigger struct {
Event string `json:"event"`
Pair string `json:"pair"`
Actions [][]string `json:"actions"`
Condition Condition `json:"condition"`
}
type Condition []interface{}
func (c *Condition) Typ() string {
return (*c)[0].(string)
}
func (c *Condition) Val() []string {
xs := (*c)[1].([]interface{})
ys := make([]string, len(xs))
for i, x := range xs {
ys[i] = x.(string)
}
return ys
}
type Triggers struct {
Collection []Trigger
}