Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON transform to String type #851

Closed
youtNa opened this issue Aug 28, 2019 · 13 comments
Closed

JSON transform to String type #851

youtNa opened this issue Aug 28, 2019 · 13 comments

Comments

@youtNa
Copy link

youtNa commented Aug 28, 2019

Input is:

{
  "key": 342,
  "data": {
    "t1": 1234,
    "t2": "test"
  }
}

Expect output is:

{
  "key": 342,
  "data": "{
    \"t1\": 1234,
    \"t2\": \"test\",
  }"
}

I have use last spec to test:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": "=toString(@(1,data))"
    }
  }
]

but it output:

{
	"key": 342,
	"data": "{t1=1234, t2=test}"
}

the output of data is not JSON.

can some one help me ? thanks

@wisthy
Copy link

wisthy commented Aug 28, 2019

basically, your expected output is not valid from a JSON point of view. It should us \n instead of line return. So something like

{
  "key": 342,
  "data": "{\n\"t1\": 1234, \n\"t2\": \"test\",\n}"
}

@youtNa
Copy link
Author

youtNa commented Aug 28, 2019

Sorry
My Expect output is:

{
  "key": 342,
  "data": "{\"t1\": 1234,\"t2\": \"test\"}"
}

data is a JSON type String.

But when i use =toString method to transform JSON, it out put like t2= test not t2:"test"

@wisthy
Copy link

wisthy commented Aug 28, 2019

I was already working with the \n, you can remove it :)

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": "=concat('{\n\"t1\": ',@(0,t1),',\n\"t2\": \"',@(0,t2),'\"\n}')"
    }
  }
]

@youtNa
Copy link
Author

youtNa commented Aug 28, 2019

thanks for your answer.

if i do not know definite key of data
maybe it is :

{
  "key": 342,
  "data": {
    "t1": 1234,
    "t2": "test",
    "d1": 20.12
  }
}

or

{
  "key": 342,
  "data": {
    "a1": "test1",
    "t2": "test",
    "c1": 472
  }
}

or some other type.

How can i do that?

@wisthy
Copy link

wisthy commented Aug 28, 2019

the point is if you don't know, how can jolt know?
in my version, I specified which fields to put quotes.
Another approach can be to put it everywhere.
Or if you have something that can be recognized in the name, we can use that.

@youtNa
Copy link
Author

youtNa commented Aug 28, 2019

Is there some method to loop keys and concat to one key.

@youtNa
Copy link
Author

youtNa commented Aug 28, 2019

Because i just want transform a json to string. So i do not need know the keys of data

@wisthy
Copy link

wisthy commented Aug 28, 2019

well, you can store the key iteself in a field as a first transformation and then concat them in a second trasnformation but not sure if you can loop

@wisthy
Copy link

wisthy commented Aug 28, 2019

will something like that help?

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": {
        "*": "=concat('\"',@(1,&),'\"')"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": "data.\"&\""
      }
    }
  }
  ,

  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": "=toString(@(1,data))"
    }
    }

]

which given one of your last input gives

{
  "key" : 342,
  "data" : "{\"t1\"=\"1234\", \"t2\"=\"test\", \"d1\"=\"20.12\"}"
}

@wisthy
Copy link

wisthy commented Aug 28, 2019

hum, Just notice that ToString is using equal and not ":"

@youtNa
Copy link
Author

youtNa commented Aug 28, 2019

yes. i think this requirement is so difficult . [cry]

@wisthy
Copy link

wisthy commented Aug 29, 2019

with a lot of tricks, got most of it right:

{
  "key" : 342,
  "data" : "{\"t1\":\"1234\",\"t2\":\"test\",\"d1\":\"20.12\"}"
}

with spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": {
          "$": "tmp.field",
          "@": "tmp.value"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "tmp": {
        "*": {
          "*": "tmp[#1].&1"
        }
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "tmp": {
        "*": {
          "tst": "=concat('\"',@(1,field),'\":\"',@(1,value),'\"')"
        }
      }
    }
  }
  ,
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "tmp": {
        "*": {
          "tst": "data"
        }
      }
    }
    },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": "=join(',', @(1,data))"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": "=concat('{', @(1,data), '}')"
    }
  }
]

There are probably way to make it more concise but wanted to move step by step

@youtNa
Copy link
Author

youtNa commented Aug 29, 2019

Thanks so mush. your response is very useful. This also has some little problem. Such as int may transform to string and null also transform to empty string.

@youtNa youtNa closed this as completed Aug 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants