Skip to content
New issue

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

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

Already on GitHub? # to your account

How to dump empty key value to yaml? #356

Closed
wclr opened this issue Jun 28, 2017 · 8 comments
Closed

How to dump empty key value to yaml? #356

wclr opened this issue Jun 28, 2017 · 8 comments

Comments

@wclr
Copy link

wclr commented Jun 28, 2017

Need to get from js:

volumes:
  data-volume:

not

volumes:
  data-volume: null

not

volumes:
  data-volume: ''

?

@puzrin
Copy link
Member

puzrin commented Jun 28, 2017

empty value === null in yaml.

No way to define such dumping style. Other styles can be tweaked via options https://github.com/nodeca/js-yaml#safedump-object---options-

@puzrin puzrin closed this as completed Jun 28, 2017
@wclr
Copy link
Author

wclr commented Jun 28, 2017

Ok, null works for me, thanks) Why not allow this with undefined by default?

@puzrin
Copy link
Member

puzrin commented Jun 28, 2017

There are no such thing as undefined in yaml. This package implements spec, not just generating pretty texts :)

@wclr
Copy link
Author

wclr commented Jul 1, 2017

@puzrin isn't there a way just to ignore undefined fields while dumping?

@puzrin
Copy link
Member

puzrin commented Jul 1, 2017

Right now - no. Dumper expects you to provide correct (dumpable) source.

There is reasonable suggestion #339, but it's not implemented.

@wclr
Copy link
Author

wclr commented Jul 1, 2017

Ok, currently I worked it around by using JSON.parse(JSON.stringify(obj)) which removes undefined.

@RachitGod
Copy link

Ok, null works for me, thanks) Why not allow this with undefined by default?

Hi,
So, How did you fix it? When i use null i get output like
key1: null

@patarapolw
Copy link

Ok, currently I worked it around by using JSON.parse(JSON.stringify(obj)) which removes undefined.

It is safer to remove undefined using https://stackoverflow.com/questions/286141/remove-blank-attributes-from-an-object-in-javascript/57625661#57625661

matter.stringify(
          content,
          Object.entries(
            programmaticallyGeneratedData,
          ).reduce((a, [k, v]) => (v === undefined ? a : { ...a, [k]: v }), {}),
        )

However, if you need to remove deep undefined, you might need https://github.com/patarapolw/deepfind#patarapolwdeepfind then delete obj[k].

martinjlowm added a commit to martinjlowm/berry that referenced this issue Jul 1, 2020
js-yaml implements the YAML spec and it has no undefined type:

- nodeca/js-yaml#356 (comment)

I.e.:

```
> require('js-yaml').safeLoad('releases:\n')
{ releases: null }
```
martinjlowm added a commit to martinjlowm/berry that referenced this issue Jul 1, 2020
js-yaml implements the YAML spec and it has no undefined type:

- nodeca/js-yaml#356 (comment)

I.e.:

```
> require('js-yaml').safeLoad('releases:\n')
{ releases: null }
```
martinjlowm added a commit to martinjlowm/berry that referenced this issue Jul 5, 2020
js-yaml implements the YAML spec and it has no undefined type:

- nodeca/js-yaml#356 (comment)

I.e.:

```
> require('js-yaml').safeLoad('')
undefined
> require('js-yaml').safeLoad('releases:\n')
{ releases: null }
```
arcanis pushed a commit to yarnpkg/berry that referenced this issue Jul 5, 2020
…es (#1536)

js-yaml implements the YAML spec and it has no undefined type:

- nodeca/js-yaml#356 (comment)

I.e.:

```
> require('js-yaml').safeLoad('')
undefined
> require('js-yaml').safeLoad('releases:\n')
{ releases: null }
```
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants