1

I am new to JSON data manipulation and I would like some help.

I have a JSON file that looks like the below:

[
  {
    "plannification": {
      "Data": 1,
      "DataType": "GB",
      "InclusionOption1": ".",
      "Default": false,
      "PropositionId": "GBT13456",
      "EssentialLink": "greatpdf.com/pdf.pdf",
      "Term": "1",
      "Segment": "part",
      "Id": "653284",
    }
  },
  {
    "plannification": {
      "Data": 1,
      "DataType": "FR",
      "inclusionOption1": ".",
      "default": false,
      "PropositionId": "FRT13456",
      "EssentialLink": "greatpdf.com/pdf2.pdf",
      "term": "1",
      "Segment": "pro",
      "Id": "984532",
    }
  }
]

I'd like to to convert this file to object and only pull data from "Segment": “pro”, as below:

    {
      984532:{
      Segment: "pro",
      EssentialLink: "greatpdf.com/pdf.pdf",
      PropositionId: "FRT13456",
     },
      etc.. {},
}

Where do I start?

2
  • any tried samples Commented Feb 27, 2018 at 5:21
  • 3
    your json is not valid. format it correctly the way you want. Commented Feb 27, 2018 at 5:27

3 Answers 3

1

You can filter your array using array#filter on the Segment where the value is pro. You can use array#reduce and iterate through the array and using Object#values() take out the values of each object and create your new object.

const data = [{ "plannification": { "Data": 1, "DataType": "GB", "InclusionOption1": ".", "Default": false, "PropositionId": "GBT13456", "EssentialLink": "greatpdf.com/pdf.pdf", "Term": "1", "Segment": "part", "Id": "653284" } }, { "plannification": { "Data": 1, "DataType":"FR", "inclusionOption1": ".", "default": false, "PropositionId": "FRT13456", "EssentialLink": "greatpdf.com/pdf2.pdf", "term": "1", "Segment": "pro", "Id": "984532", } } ],
      result = data.filter(o => {
        let { Segment } = Object.values(o)[0];
        return Segment === 'pro';
      }).reduce((r,o) => {
        let { Id, Segment, PropositionId, EssentialLink } = Object.values(o)[0];
        r[Id] = { Segment, PropositionId, EssentialLink };
        return r;
      },{})
console.log(result);

Sign up to request clarification or add additional context in comments.

2 Comments

Great! thank you so much. This is exactly what I am looking for. Now I'll find how to pull from a file
Using Javascript, you can refer to this
0

1 - Read the json file

2 - Iterate over the array of objects

3 - For every object, check your desired property

4 - If it matchs what you want, read the properties of the object and construct your object

Comments

0

your data structure is not valid, at first correct the way you've written your json array of object so that i can figure out

1 Comment

This is unfortunately from an API, I cannot change the structure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.