DEV Community

udomsak
udomsak

Posted on

4 1

It's possible to pare JSON via DSL or dynamic input template in Golang

Example

Input

{
   "name" : "udomsak",
   "dataForm" : { 
     "attribute1": "myname is"
     "gender" : "male",
     "location": "Mars"
}

Parser Language

This parser language is Dynamic and does not need to compile in Struct in Golang. that mean can get from database or file use as parser.

ParserGet:
  - name
  - dataForm.gender 
  - dataForm.location

Example request

ProjectA:

http://api.example.com/parser-rule/parser1
  - parser1 load from parser rule file :  "parser1.rule.conf"

ProjectB:
http://api.example.com/parser-rule/parser2
  - parser1 load from parser rule file :  "parser2.rule.conf"

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (2)

Collapse
 
vorsprung profile image
vorsprung

OK, what you haven't explained is that in Go, JSON parsing can occur directly into structs

With your example, this is complicated with the need for a struct inside a struct

package main

import (
    "encoding/json"
    "fmt"
)

type DataForm struct {
    Attribute1 string `json:"attribute1"`
    Gender     string `json:"gender"`
    Location   string `json:"location"`
}

type Example struct {
    Name      string   `json:"name"`
    DataFormd DataForm `json:"dataForm"`
}

var testdata = `{
  "name": "udomsak",
  "dataForm": {
    "attribute1": "myname is",
    "gender": "male",
    "location": "Mars"
  }
}`

func main() {
    var data Example

    json.Unmarshal([]byte(testdata), &data)

    fmt.Printf("Hello, %v", data.DataFormd.Attribute1)
}

The problem is that JSON can contain all kinds of fields and nested data and Go requires the schema to be declared ahead of time with struct declarations

However it's possible to load arbitrary JSON into Go anyway

Just load it into an empty interface

var data interface{}

json.Unmarshal([]byte(testdata), &data)

but then accessing fields in the data becomes a major pita. To achieve the same as above...

fmt.Printf("Hello, %v", (data.(map[string]interface{}))["dataForm"].(map[string]interface{})["attribute1"])
Collapse
 
udomsak profile image
udomsak

@vorsprung thank you for answer me and sorry for very lately reply. I'm so busy. But for this problem look like i must use custom parser to work with this case. ( goyacc ).

my intend is you json as form ( json form-schema ) and anyone can create they form as need. that mean in server-side they need implement custom parser to get attribute they need. :)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay